Example: Ordered Analytical Functions Using QUALIFY with RANK - Teradata VantageCloud Lake

Lake - Working with SQL

Deployment
VantageCloud
Edition
Lake
Product
Teradata VantageCloud Lake
Release Number
Published
February 2025
ft:locale
en-US
ft:lastEdition
2025-11-21
dita:mapPath
jbe1714339405530.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
jbe1714339405530

Adding a QUALIFY clause to a query eliminates rows from an unqualified table.

For example, if you wanted to see whether the high sales months were unusual, you can add a QUALIFY clause to the previous query.

   SELECT item, smonth, sales, 
   RANK() OVER (PARTITION BY item ORDER BY sales DESC), 
   AVG(sales) OVER (PARTITION BY item ORDER BY smonth ROWS 3 PRECEDING)
   FROM sales_tbl
   ORDER BY item, smonth
   QUALIFY RANK() OVER(PARTITION BY item ORDER BY sales DESC) <=5;

This additional qualifier produces a results table that may look like the following.

Item SMonth Sales Rank(Sales) Moving Avg(Sales)
A 1996-04 210 3 155
A 1996-05 270 1 195
A 1996-06 250 2 225
A 1996-07 190 4 230
A 1996-08 180 5 222
B 1996-02 30 1 30
... ... ... ... ...

The result indicates that sales were likely low before the start of the current sales season.