Window Function Examples
Topics
- AVG Window Function Examples
- COUNT Window Function Examples
- CUME_DIST Window Function Examples
- DENSE_RANK Window Function Examples
- FIRST_VALUE and LAST_VALUE Window Function Examples
- LAG Window Function Examples
- LEAD Window Function Examples
- LISTAGG Window Function Examples
- MAX Window Function Examples
- MEDIAN Window Function Examples
- MIN Window Function Examples
- NTH_VALUE Window Function Examples
- NTILE Window Function Examples
- PERCENT_RANK Window Function Examples
- PERCENTILE_CONT Window Function Examples
- PERCENTILE_DISC Window Function Examples
- RANK Window Function Examples
- RATIO_TO_REPORT Window Function Examples
- ROW_NUMBER Window Function Example
- STDDEV_POP and VAR_POP Window Function Examples
- SUM Window Function Examples
- Unique Ordering of Data for Window Functions
This section provides examples for using the window functions.
Some of the window function examples in this section use a table named WINSALES, which contains 11 rows:
SALESID | DATEID | SELLERID | BUYERID | QTY | QTY_SHIPPED |
---|---|---|---|---|---|
30001 | 8/2/2003 | 3 | B | 10 | 10 |
10001 | 12/24/2003 | 1 | C | 10 | 10 |
10005 | 12/24/2003 | 1 | A | 30 | |
40001 | 1/9/2004 | 4 | A | 40 | |
10006 | 1/18/2004 | 1 | C | 10 | |
20001 | 2/12/2004 | 2 | B | 20 | 20 |
40005 | 2/12/2004 | 4 | A | 10 | 10 |
20002 | 2/16/2004 | 2 | C | 20 | 20 |
30003 | 4/18/2004 | 3 | B | 15 | |
30004 | 4/18/2004 | 3 | B | 20 | |
30007 | 9/7/2004 | 3 | C | 30 |
The following script creates and populates the sample WINSALES table.
create table winsales( salesid int, dateid date, sellerid int, buyerid char(10), qty int, qty_shipped int); insert into winsales values (30001, '8/2/2003', 3, 'b', 10, 10), (10001, '12/24/2003', 1, 'c', 10, 10), (10005, '12/24/2003', 1, 'a', 30, null), (40001, '1/9/2004', 4, 'a', 40, null), (10006, '1/18/2004', 1, 'c', 10, null), (20001, '2/12/2004', 2, 'b', 20, 20), (40005, '2/12/2004', 4, 'a', 10, 10), (20002, '2/16/2004', 2, 'c', 20, 20), (30003, '4/18/2004', 3, 'b', 15, null), (30004, '4/18/2004', 3, 'b', 20, null), (30007, '9/7/2004', 3, 'c', 30, null);