I would like to generate an index row in a table that may contain duplicates. the index need to be based on values from the table and last update date.
The data looks like this:
ID Val1 LastUpdateDate
-- ------ -------------
1 0 07.09.2019
1 2.5 12.09.2019
1 2.5 27.09.2019
1 3.5 01.10.2019
1 2.5 24.10.2019
1 0 01.11.2019
I would like to have:
ID Val1 LastUpdateDate index
-- ------ ------------- ----
1 0 07.09.2019 1
1 2.5 12.09.2019 2
1 2.5 27.09.2019 2
1 3.5 01.10.2019 3
1 2.5 24.10.2019 4
1 0 01.11.2019 5
I've tried with the following code but it's not working:
SELECT ID
,Value1
,Value2
,Value3
,LastUpdateDate
,(ROW_NUMBER() OVER (PARTITION BY ID ORDER BY last_update_date) - ROW_NUMBER()OVER(PARTITION BY ID,Value1,Value2,Value3 ORDER BY ID,Value1,Value2,Value3)) AS index
FROM Table1
ORDER BY LastUpdateDate