次の名前と日付のペアごとに上位 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
簡単な方法があると思ったのですが、わかりません!