Q
Problem solve Get help with specific problems with your technologies, process and projects.

# What does this theta self-join mean?

Could you please explain to me the DB2 query given below?? It is a self join, but the part which I cannot understand is how it manages to generate sequential order numbers with the help of self join and where clause.

```SELECT count(*) + 1000,
A.SLS_TM_NBR,
A.FFO_TYP_CD,
A.FFO_NBR
FROM
WHERE
((A.SLS_TM_NBR > B.SLS_TM_NBR) OR
(A.SLS_TM_NBR = B.SLS_TM_NBR AND
?? A.FFO_TYP_CD > B.FFO_TYP_CD) OR
(A.SLS_TM_NBR = B.SLS_TM_NBR AND
?? A.FFO_TYP_CD = B.FFO_TYP_CD AND
?? A.FFO_NBR >= B.FFO_NBR))
GROUP BY
A.SLS_TM_NBR,
A.FFO_TYP_CD,
A.FFO_NBR
```

Don't let the complex join condition throw you. This self-join simply counts rows which are "less than" the row in question. This type of join is not an equi-join, where rows are matched that have equal values in some key. Rather, each row is joined to all the rows that satisfy the inequality. This is called a theta join even though few people use this terminology. (I just call it a "less than" join.) With each row joined to all its lesser rows, the GROUP BY generates a COUNT(*) which simply indicates how many lesser rows each row has.

One example where this type of join is useful is in determining a ranking. Imagine a table like this --

```theID  theValue
a       6
b       8
c      12
d       7
e      11
f      15
g      13
h      21
i       9```

Running a simple query with an ORDER BY will get you the records in the right sequence, but using a theta self-join will generate rankings. Remember that the ranking is actually just the number of lesser rows --

```select A.theValue
, count(*) as theRank
from theTable as A
, theTable as B
where A.theValue >= B.theValue
group by
A.theValue

theValue theRank
6       1
7       2
8       3
9       4
11       5
12       6
13       7
15       8
21       9```

Notice that the results are ranked from smallest to largest. This would be the case when ranking golf scores or demerit points, say. For other queries, where the top rank is the largest, the theta condition would be a "greater than" comparison instead of "less than." Also, notice I had to use "less than or equal to" to generate a result row for every row in the A table. Otherwise, the lowest row is left out, as there are no rows lower than the lowest.

Warning: the above query will work correctly only if there are no "ties." If you have ties in your data, the ranking query is substantially more complex.

So, given that the theta join conveniently produces a number, which may or may not be a ranking, but which in your example was further massaged by adding three zeroes to it, is this a good way to generate order numbers? The answer is maybe. It is not a good method if there are ties, because then there will be more than one row with the same number of lesser rows, and hence the same order number!! For your own comfort, double-check to make sure there are UNIQUE constraints on the table columns that are used in the WHERE clause.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### DataStax making Cassandra easier with Kubernetes Operators

New Kubernetes tool for the Apache Cassandra database is DataStax's attempt to create a broadly adopted approach for cloud native...

• ### Ascend boosts enterprise data governance with data lineage

The new capabilities of Ascend Govern bring data lineage and enterprise data governance as well as the ability to try and figure ...

• ### The business benefits of enterprise data governance and MDM

Data leaders from prominent large organizations provide insights into data governance best practices and benefits, at ...

• ### Many employees still resist data-driven culture

While most employees see the benefits of data to drive decision-making, many still resist analytics, according to a report from ...

• ### SAS analytics platform benefiting from AI investment

Fueled by SAS' investment in AI and augmented intelligence features, the long-established vendor strengthened its BI platform ...

• ### Trends and top use cases for streaming data analytics

As more enterprises adopt real-time analytics, new infrastructure and best practices are appearing. Here are some trending ...

## SearchSAP

• ### SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2027, organizations must make some tough decisions. Here's a look at your choices.

• ### How to pick a SuccessFactors implementation partner

Selecting the right SuccessFactors implementation partner is a critical step in the journey to deploying the system. Check out ...

• ### 6 critical steps of a successful SAP S/4HANA migration

A successful S/4HANA project starts with knowing why your organization should even make the move, then nailing down whether S/4 ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Developing a COVID-19 application? Design it the right way

Application developers with skills and an idea could design an app to combat the virus. Follow these insights to effectively ...

• ### How developers can volunteer during the COVID-19 pandemic

What resources can developers offer to go against COVID-19? Explore some promising developer volunteer and open source project ...

• ### 5 Jenkins alternatives for Java developers

In need of an alternative to Jenkins as part of a CI/CD environment? Consider options from Atlassian, GitLab, Spinnaker, ...

## SearchDataCenter

• ### Tech giants aim supercomputers, clouds at finding a COVID-19 cure

The new COVID-19 HPC consortium comprised of tech giants, national labs and academia are using supercomputers and clouds to speed...

• ### Top vendor SDDC certification programs for admins

If you decide to grow your knowledge of software-defined data center technologies, VMware, Nuage Networks and Microsoft offer ...

• ### Build up your knowledge of leaf-spine network technology

As organizations deal with challenges of single tree path networks, many admins are considering leaf-spine architectures. Learn ...

## SearchContentManagement

• ### Creating digital experiences key to driving exceptional CX

The way in which businesses build digital experiences is ever-evolving. It's a race to create easier-to-use customer interfaces ...

• ### Vendors offer free remote work technology for telecommuting

It's more than a marketing ploy: Cloud technology vendors, seeing their own issues enabling remote work, extend paid products to ...

• ### How businesses should deal with enterprise search issues

Enterprise search issues frequently complicate user experience with ECM systems. While users may face many problems, they also ...

## SearchHRSoftware

• ### Best-case, worst-case pandemic scenarios for India service providers

India's IT and business support services will be tested during that country's pandemic lockdown. Many workers are now operating ...

• ### Farmers Insurance piloting human-like VR training

VR training has become a best practice at Farmers Insurance Exchange. Its claims adjusters are being exposed to more scenarios ...

• ### Virtual hiring, avoiding layoffs part of HR's pandemic response

In the face of a pandemic, many firms are trying to avoid layoffs and even hire. They are using virtual tools, including testing ...

Close