您当前位置:
首页 >
数据库 >
Sqlserver > SQL 2005使用PARTITION and Rank实现group by to
SQL 2005使用PARTITION and Rank实现group by to
来源:程序员人生 发布时间:2014-04-10 14:19:25 阅读次数:3222次
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
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠