Hi I have a data table. I want to output dense_rank names starting from the first group of names according to the order of the sorted dates. eg.
DROP TABLE MyTable SELECT * INTO MyTable FROM ( VALUES ('2015-12-23', 'ccc'),('2015-12-21', 'aaa'), ('2015-12-20', 'bbb'),('2015-12-22', 'aaa') ) t (date, name) SELECT DENSE_RANK() OVER (ORDER BY name) rank, * FROM MyTable ORDER BY date
For the request above, I received
rank date name 2 2015-12-20 bbb 1 2015-12-21 aaa 1 2015-12-22 aaa 3 2015-12-23 ccc
You can see that the dates are sorted (good), the ranks are assigned to the names in the group (good), but the ranks do not start with the first group of names, for example. I want
rank date name 1 2015-12-20 bbb 2 2015-12-21 aaa 2 2015-12-22 aaa 3 2015-12-23 ccc
How do you fix the request? If there are several working answers, the simplest / shortest will be selected as the answer. Thanks.
Added:
Thanks to @ lad2025 and @GiorgosBetsos for clarifying my question. Sorting is done directly according to dates and dates that are unique in my case. Names may be repeated and not displayed sequentially. So with ('2015-12-24', 'aaa') , output
rank date name 1 2015-12-20 bbb 2 2015-12-21 aaa 2 2015-12-22 aaa 3 2015-12-23 ccc 4 2015-12-24 aaa
sql tsql dense-rank sql-server-2008-r2
user1589188
source share