如何仅返回以下每个 Name-datee 对的前 3 个值?
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
我以为会有一个简单的方法,但无法弄清楚!