Example: Subqueries Using the RANK Functions and QUALIFY Clauses - Teradata Database - Teradata Vantage NewSQL Engine

SQL Data Manipulation Language

Product
Teradata Database
Teradata Vantage NewSQL Engine
Release Number
16.20
Published
March 2019
Language
English (United States)
Last Update
2019-05-03
dita:mapPath
fbo1512081269404.ditamap
dita:ditavalPath
TD_DBS_16_20_Update1.ditaval
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata Vantage™

The following example uses nested RANK functions and QUALIFY clauses to report the top 100 items by profitability and the top items by revenue, matching those that appear in both lists using an OUTER JOIN:

     SELECT *
     FROM (SELECT item, profit, RANK(profit) AS profit_rank
           FROM item, sales
           QUALIFY profit_rank <= 100 AS p)
     FULL OUTER JOIN
          (SELECT item, revenue, RANK(revenue) AS revenue_rank
           FROM item, sales
           QUALIFY revenue_rank <= 100 AS r)
     ON p.item = r.item;

The results of this query might look something like the following table:

item

----

profit

------

profitrank

----------

item

----

revenue

-------

revenuerank

-----------

Dress slacks 17804 74 Dress slacks 180211 56
Dress shirts 16319 68 ? ? ?
Dresses 55888 82 Dresses 652312 77
Blouses 9849 48 Blouses 771849 92
? ? ? Skirts 817811 55