sql server - Select top 2 distinct for each id and date -
i have table :
table1:
[id] [tdate] [score] 1 1.1.00 50 1 1.1.00 60 2 1.1.01 50 2 1.1.01 70 2 1.3.01 40 3 1.1.00 80 3 1.1.00 30 3 1.2.00 40
my desired output should this:
[id] [tdate] [score] 1 1.1.00 60 2 1.1.01 70 2 1.3.01 40 3 1.1.00 80 3 1.2.00 40
so fare, have written this:
select distinct top 2 id, tdate, score ( select id, tdate, score, row_number() over(partition tdate order score) od table1 ) a.od = 1 order score
but gives me :
[id] [tdate] [score] 2 1.1.01 70 3 1.1.00 80
of course can this:
"select top 2 ...where id = 1"
and then:
union `"select top 2 ... id = 2"`
etc.. have 100,000 of this..
any way generalize id? thank you.
with toptwo ( select id, tdate, score, row_number() on ( partition tdate order score ) rowno [table_name] ) select * toptwo rowno <= 2
Comments
Post a Comment