程序员人生 网站导航

SQL 2005使用PARTITION and Rank实现group by to

栏目:Sqlserver时间:2014-04-10 14:19:25
DEMO1:
select * from
(
select Player, Year, HomeRuns, Rank() over (Partition BY Player order by HomeRuns DESC, Year DESC) as Rank
from
Batting
) tmp
where Rank = 1

DEMO2:
WITH ttts AS (

Select JournalID,ParentID,RANK() OVER (partition by ParentID orDER BY JournalID asc) AS rt From Journal_Category Where ParentID in (Select JournalID from Journal_Category where ParentID = 1) Group By ParentID,JournalID

)

Select * from ttts where ttts.rt <=1

轻松实现Group By Top

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐