Skip to content

Commit a21c2ee

Browse files
committed
add ranking example
1 parent 3a94052 commit a21c2ee

3 files changed

+1
-16
lines changed

intermediate_sql_class_notes.docx

-145 Bytes
Binary file not shown.

intermediate_sql_class_notes.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,22 +1406,7 @@ SELECT * FROM PRODUCT_SALES_BY_CUSTOMER
14061406
WHERE RANKING <= 3
14071407
```
14081408

1409-
Note if you want identical values to receive the same ranking, use the `rank()` function instead of `row_number()`.
1410-
1411-
```sql
1412-
WITH TOTAL_QTYS AS (
1413-
SELECT CUSTOMER_ID, PRODUCT_ID, SUM(QUANTITY) AS TOTAL_QTY
1414-
FROM CUSTOMER_ORDER
1415-
GROUP BY 1,2
1416-
),
1417-
1418-
PRODUCT_SALES_BY_CUSTOMER AS (
1419-
SELECT CUSTOMER_ID, PRODUCT_ID, TOTAL_QTY,
1420-
RANK() OVER (PARTITION BY CUSTOMER_ID ORDER BY TOTAL_QTY DESC) AS RANKING
1421-
FROM TOTAL_QTYS
1422-
)
1423-
SELECT * FROM PRODUCT_SALES_BY_CUSTOMER
1424-
WHERE RANKING <= 3
1409+
Note if you want identical values to receive the same ranking, use the `RANK()` function instead of `row_number()`. Use `DENSE_RANK()` if you want to force the values to be consecutive rather than dupes causing ranks to be skipped.
14251410

14261411

14271412
# EXERCISE

intermediate_sql_class_notes.pdf

-176 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)