SAP HANA SQL ROW_NUMBER Function
Sequentially numbers the rows within a partition of a result set, with the first row of each partition assigned as 1.
let`s see the example to understand better.
ROW_NUMBER () OVER (PARTITION BY FIELD ORDER BY FIELD DESC)
Create Hana Table:
CREATE ROW TABLE Product (ProdName VARCHAR(50), Type VARCHAR(20), Sales INT);
INSERT INTO Product VALUES(‘Tee Shirt’,’Plain’,21);
INSERT INTO Product VALUES (‘Tee Shirt’,’Lettered’,22);
INSERT INTO Product VALUES (‘Tee Shirt’,’Team logo’,30);
INSERT INTO Product VALUES(‘Hoodie’,’Plain’,60);
INSERT INTO Product VALUES (‘Hoodie’,’Lettered’,65);
INSERT INTO Product VALUES (‘Hoodie’,’Team logo’,80);
INSERT INTO Product VALUES(‘Ballcap’,’Plain’,8);
INSERT INTO Product VALUES (‘Ballcap’,’Lettered’,40);
INSERT INTO Product VALUES (‘Ballcap’,’Team logo’,27);
Create Row umber
SELECT ProdName, Type, Sales,
ROW_NUMBER() OVER (PARTITION BY ProdName ORDER BY Sales DESC) AS row_num
FROM Product
ORDER BY ProdName, Sales DESC;
| PRODNAME | DESCRIPTION | SALES | ROW_NUM |
| Ballcap | Lettered | 40 | 1 |
| Ballcap | Team logo | 27 | 2 |
| Ballcap | Plain | 8 | 3 |
| Hoodie | Team logo | 80 | 1 |
| Hoodie | Lettered | 65 | 2 |
| Hoodie | Plain | 60 | 3 |
| Tee Shirt | Team logo | 30 | 1 |
| Tee Shirt | Lettered | 22 | 2 |
| Tee Shirt | Plain | 21 | 3 |
