QUALIFY Clause Examples | SQL SELECT Statements | Teradata Vantage - Example: Using the RANK Function in a QUALIFY Clause - Advanced SQL Engine - Teradata Database

SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
qtb1554762060450.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata Vantage™

The following statement displays each item in a sales table, its total sales, and its rank within the top 100 selling items:

     SELECT itemid, sumprice, RANK() OVER (ORDER BY sumprice DESC)
     FROM (SELECT a1.item_id, SUM(a1.sale)
           FROM sales AS a1
           GROUP BY a1.itemID) AS t1 (item_id, sumprice)
     QUALIFY RANK() OVER (ORDER BY sum_price DESC) <=100;