Create Identifier/counter Based On Some Shared Columns And Seperate Based On Other Columns
I want to create a calculated column based on a shared column but the calculated column should 'restart' and be grouped based on a third column. As described in the picture below C
Solution 1:
Use windowing function DENSE_RANK() with an OVER() clause:
DECLARE@tblTABLE(Column1 INT,Column2 INT,Column3 VARCHAR(100));
INSERTINTO@tblVALUES(1,1,'A')
,(1,2,'A')
,(1,3,'B')
,(2,1,'A')
,(2,2,'A')
,(2,3,'B')
,(3,1,'A')
,(3,2,'B')
,(3,3,'V');
SELECT*
,DENSE_RANK() OVER(PARTITIONBY Column1 ORDERBY Column3) AS ComputedColumn
FROM@tbl;
The PARTITION BY will re-start the counter for each new value in column1, while the ORDER BY defines the ranking.
Hint: Do not paste pictures!
For your next question please follow my example to create a stand-alone example reproducing your issue and add the code you've tried yourself.
Post a Comment for "Create Identifier/counter Based On Some Shared Columns And Seperate Based On Other Columns"