Skip to content Skip to sidebar Skip to footer

Row_number() Over Partition - Easier Way

I have the below query: INSERT into @TEST (col1,staffnumber,rn) SELECT starttime as col1,staffnumber,ROW_NUMBER() OVER (PARTITION BY StaffNumber ORDER BY STARTTIME DESC) AS rn FR

Solution 1:

Compute the ROW_NUMBER after the UNION.

SELECT*, ROW_NUMBER() OVER (PARTITIONBY StaffNumber ORDERBY STARTTIME DESC) AS rn
FROM (
 SELECT*FROM T1
 UNIONALLSELECT*FROM T2
) x

I suspect you need UNION ALL.

Post a Comment for "Row_number() Over Partition - Easier Way"