sql server - T-SQL: Get top n records by column -
struggling what's simple problem. have query this:
;with rankeddata ( -- big, complex subquery) select userid, attributeid, itemid rankeddata rank = 1 order dateventdate desc
the sub-query designed grab big chunk of interlined data , rank itemid , date, rank=1 in above query ensures unique itemids, ordered date. partition is:
rank() on (partition itemid order dateventdate desc) rk
the problem want top 75 records each userid, ordered date. seeing i've got rank inside sub-query sort out item duplicates date, can't see straightforward way of doing this.
cheers, matt
i think query should like
select t.userid, t.attributeid, t.itemid ( select userid, attributeid, itemid, rowid = row_number() on ( partition userid order dateventdate ) rankeddata ) t t.rowid <= 75
Comments
Post a Comment