如何在以下每个Name-datee对中仅返回前三个值?
DECLARE @t TABLE(NAME NVARCHAR(MAX),datee date,val money)
insert INTO @t SELECT 'a','2012-01-02',100
insert INTO @t SELECT 'a','2012-01-02',100
insert INTO @t SELECT 'a','2012-01-03',100
insert INTO @t SELECT 'a','2012-01-05',150
insert INTO @t SELECT 'a','2012-01-06',200
insert INTO @t SELECT 'b','2012-01-07',200
insert INTO @t SELECT 'b','2012-01-07',400
insert INTO @t SELECT 'b','2012-01-09',500
insert INTO @t SELECT 'b','2012-01-12',600
insert INTO @t SELECT 'b','2012-01-13',100
SELECT Name, datee, SUM(val) sumval from @t
GROUP BY rollup(NAME ,datee)
order by Name, sumval desc
此当前版本返回:
Name datee sumval
NULL NULL 2450.00
a NULL 650.00
a 2012-01-02 200.00
a 2012-01-06 200.00
a 2012-01-05 150.00
a 2012-01-03 100.00
b NULL 1800.00
b 2012-01-07 600.00
b 2012-01-12 600.00
b 2012-01-09 500.00
b 2012-01-13 100.00
我想回复:
Name datee sumval
NULL NULL 2450.00
a NULL 650.00
a 2012-01-02 200.00
a 2012-01-06 200.00
a 2012-01-05 150.00
b NULL 1800.00
b 2012-01-07 600.00
b 2012-01-12 600.00
b 2012-01-09 500.00
我认为这是一种简单的方法,但无法解决这个问题!
答案 0 :(得分:1)
或许row_number
使用subquery
可以达到您的效果:
select *
from (
select *,
row_number() over (partition by name order by sumval desc) rn
from (
select Name, datee, SUM(val) sumval
from @t
group by rollup(NAME ,datee)
order by Name, sumval desc
) t
) t
where rn <= 3 or datee is null
您可以改为使用row_number() over (partition by name order by sum(val))
并删除其中一个子查询。
select *
from (
select Name, datee, SUM(val) sumval,
row_number() over (partition by name order by SUM(val) desc) rn
from @t
group by rollup(NAME ,datee)
order by Name, sumval desc
) t
where rn <= 3 or datee is null
答案 1 :(得分:1)
;with cteBase as (
Select Name
,datee
,sumval=SUM(val)
,rowNr=ROW_NUMBER() over (Partition By Name Order by sum(Val) Desc)
From @t
GROUP BY rollup(NAME ,datee)
)
Select *
From cteBase
Where RowNr<=4
order by Name, sumval desc
返回
Name datee sumval rowNr
NULL NULL 2450.00 1
a NULL 650.00 1
a 2012-01-02 200.00 2
a 2012-01-06 200.00 3
a 2012-01-05 150.00 4
b NULL 1800.00 1
b 2012-01-07 600.00 2
b 2012-01-12 600.00 3
b 2012-01-09 500.00 4
答案 2 :(得分:0)
您需要使用RANK()
或ROW_NUMBER()
来索引输出,然后您需要将其包装到子查询中,并过滤掉您需要的任何数字以上的任何行。
像这样:
SELECT
t.Name,
t.date,
t.sumval
FROM
(
SELECT
Name,
date,
SUM(val) sumval,
ROW_NUMBER() OVER (ORDER BY sumval, PARTITION BY name) RowId
from @t GROUP BY rollup(NAME ,datee)
) t
WHERE RowId <= 3
order by Name, sumval desc
答案 3 :(得分:0)
由于rollup
,这是一个很复杂的问题。我认为以下内容可行:
SELECT t.*
FROM (SELECT Name, datee, SUM(val) as sumval,
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY SUM(val) DESC) as seqnum
FROM @t t
GROUP BY rollup(NAME, datee)
) t
WHERE seqnum <= 3 or Datee is NULL
ORDER BY Name, sumval desc;
如果这不起作用(我现在无法测试),这将:
SELECT Name, datee, sumval
FROM (SELECT Name, datee, SUM(sumval) as sumval, MAX(seqnum) as seqnum
FROM (SELECT Name, datee, SUM(val) as sumval,
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY SUM(val) DESC) as seqnum
FROM @t t
GROUP BY Name, datee
) t
GROUP BY rollup(NAME, datee)
) t
WHERE seqnum <= 3 or Date is NULL
ORDER BY Name, sumval desc;