日期:2014-05-18 浏览次数:20494 次
--create table sp(id int,spname nvarchar(10))
--create table ck(ckname nvarchar(10))
--create table sj(d int,m int,y int)
create table fh(dt datetime,spname nvarchar(10),ckname nvarchar(10),sl int)
insert into fh select '2010-10-10','A','AA',200
insert into fh select '2010-10-10','A','BB',300
insert into fh select '2010-10-10','A','CC',125
insert into fh select '2010-10-11','B','AA',170
insert into fh select '2010-10-10','B','AA',220
go
select dt,spname,ckname,sl,rm as 排名 from (
select ROW_NUMBER() over(partition by dt,spname order by sum(sl)) as rm,dt,spname,ckname,sum(sl) as sl from fh
group by dt,spname,ckname
)t where rm <=10
go
drop table fh
/*
dt spname ckname sl 排名
----------------------- ---------- ---------- ----------- --------------------
2010-10-10 00:00:00.000 A CC 125 1
2010-10-10 00:00:00.000 A AA 200 2
2010-10-10 00:00:00.000 A BB 300 3
2010-10-10 00:00:00.000 B AA 220 1
2010-10-11 00:00:00.000 B AA 170 1
(5 行受影响)
*/