日期:2014-05-18  浏览次数:20512 次

一个 分组排序取前N条记录的问题,困扰一天了,晕
数据如下:

 id code   tdate cname    tcount
77037 m1301 2012-1-18 上海大陆 46
77038 m1301 2012-1-18 中粮期货 8
77039 m1301 2012-1-18 鲁证期货 8
77040 m1301 2012-1-18 创元期货 7
77041 m1301 2012-1-18 万达期货 5
84182 m1302 2012-1-19 上海大陆 51
84183 m1302 2012-1-19 中粮期货 18
84184 m1302 2012-1-19 鲁证期货 12
84185 m1302 2012-1-19 银河期货 12
84186 m1302 2012-1-19 创元期货 7
84187 m1302 2012-1-19 万达期货 4
91272 m1301 2012-1-20 上海大陆 70
91273 m1301 2012-1-20 一德期货 42
91274 m1301 2012-1-20 万达期货 40
91275 m1301 2012-1-20 重庆新涪 35
91276 m1301 2012-1-20 招商期货 23
91277 m1301 2012-1-20 中粮期货 18
91278 m1301 2012-1-20 银河期货 16
91279 m1301 2012-1-20 广州期货 13
91280 m1301 2012-1-20 鲁证期货 11
91281 m1301 2012-1-20 创元期货 7
91282 m1301 2012-1-20 神华期货 5
91283 m1301 2012-1-20 长江期货 4
91284 m1301 2012-1-20 中州期货 4
91285 m1301 2012-1-20 中证期货 4


1.要求列出 每种Code,每天的数量在前五名的 明细(包括id,code,tdate,cname,tcount)
2.求出 每种Code,每天的数量在前五名的,数量之和.

请SQL高手们帮忙


------解决方案--------------------
SELECT * FROM TB T
 WHERE tcount IN(SELECT TOP 5 tcount FROM TB WHERE CODE=T.CODE AND DATE=T.DATE ORDER BY tcount DESC)

求和就子查询
------解决方案--------------------
SQL code
select *,row_number() over(order by cname,tcount) as rownumber
from tb
where rownumber <=5

------解决方案--------------------
SQL code

--> 测试数据:[test]
if object_id('[test]') is not null 
drop table [test]
create table [test](
[id] int,
[code] varchar(5),
[tdate] datetime,
[cname] varchar(8),
[tcount] int
)
go
insert [test]
select 77037,'m1301','2012-1-18','上海大陆',46 union all
select 77038,'m1301','2012-1-18','中粮期货',8 union all
select 77039,'m1301','2012-1-18','鲁证期货',8 union all
select 77040,'m1301','2012-1-18','创元期货',7 union all
select 77041,'m1301','2012-1-18','万达期货',5 union all
select 84182,'m1302','2012-1-19','上海大陆',51 union all
select 84183,'m1302','2012-1-19','中粮期货',18 union all
select 84184,'m1302','2012-1-19','鲁证期货',12 union all
select 84185,'m1302','2012-1-19','银河期货',12 union all
select 84186,'m1302','2012-1-19','创元期货',7 union all
select 84187,'m1302','2012-1-19','万达期货',4 union all
select 91272,'m1301','2012-1-20','上海大陆',70 union all
select 91273,'m1301','2012-1-20','一德期货',42 union all
select 91274,'m1301','2012-1-20','万达期货',40 union all
select 91275,'m1301','2012-1-20','重庆新涪',35 union all
select 91276,'m1301','2012-1-20','招商期货',23 union all
select 91277,'m1301','2012-1-20','中粮期货',18 union all
select 91278,'m1301','2012-1-20','银河期货',16 union all
select 91279,'m1301','2012-1-20','广州期货',13 union all
select 91280,'m1301','2012-1-20','鲁证期货',11 union all
select 91281,'m1301','2012-1-20','创元期货',7 union all
select 91282,'m1301','2012-1-20','神华期货',5 union all
select 91283,'m1301','2012-1-20','长江期货',4 union all
select 91284,'m1301','2012-1-20','中州期货',4 union all
select 91285,'m1301','2012-1-20','中证期货',4
go

;with t
as(
select 
    px=DENSE_RANK()over(partition by [code] order by tcount desc),
    *
from
    test
)
--问题一:
select 
    id,
    code,
    tdate,
    cname,
    tcount
from
    t
where 
    px<=5
go
/*
id    code    tdate    cname    tcount
-----------------------------
91272    m1301    2012-01-20 00:00:00.000    上海大陆    70
77037    m1301    2012-01-18 00:00:00.000    上海大陆    46
91273    m1301    2012-01-20 00:00:00.000    一德期货    42
91274    m1301    2012-01-20 00:00:00.000    万达期货    40
91275    m1301    2012-01-20 00:00:00.000    重庆新涪    35
84182    m1302    2012-01-19 00:00:00.000    上海大陆    51
84183    m1302    2012-01-19 00:00:00.000    中粮期货    18
84184    m1302    2012-01-19 00:00:00.000    鲁证期货    12
84185    m1302    2012-01-19 00:00:00.000    银河期货    12
84186    m1302    2012-01-19 00:00:00.000    创元期货    7
84187    m1302    2012-01-19 00:00:00.000    万达期货    4
*/
--问题二
;w