SQL Server Rank SQL for multiple conditions

mftmpeh8  于 2024-01-05  发布在  其他
关注(0)|答案(1)|浏览(123)

I have this output from my 1st query, and i wont know the rank row for each session over contactID and service ID.

Each ContactID + ServiceID need group to obtain rank over sessionID by createdDate Desc.

My data:

My query

SELECT ROW_NUMBER() OVER (PARTITION BY ServiceID, ContactID , SESSIONID ORDER BY [Created] DESC) AS rn1,
        RANK() OVER (PARTITION BY ServiceID, ContactID , SESSIONID ORDER BY [Created] DESC) AS rn2,
        DENSE_RANK() OVER (PARTITION BY ServiceID, ContactID , SESSIONID ORDER BY [Created] DESC) AS rn3 
 FROM #TABLE

My output its not what I need.

I need some like this:

8ftvxx2r

8ftvxx2r1#

Looks like the date is irrelevant to the calculation, you just want DENSE_RANK sorted by sessionID

DENSE_RANK() OVER (PARTITION BY ServiceID, ContactID ORDER BY SESSIONID DESC) AS Rank

相关问题