我想使用ROLLUP將輸出的小計添加到此代碼的底部,但它失敗,說它們不在組中。我不希望他們分組,我只是想讓小計在底部。SQL:沒有分組彙總
SELECT Date, HomeTeam, AwayTeam, FTHG, FTAG, FTR,
CASE WHEN HomeTeam = 'Arsenal' THEN
CASE FTR WHEN 'H' THEN 3 WHEN 'D' THEN 1 WHEN 'A' THEN 0 ELSE 0 END
WHEN AwayTeam = 'Arsenal' THEN
CASE FTR WHEN 'H' THEN 0 WHEN 'D' THEN 1 WHEN 'A' THEN 3 ELSE 0 END
ELSE 0
END
AS Points, CASE WHEN HomeTeam = 'Arsenal' THEN FTHG - FTAG
WHEN AwayTeam = 'Arsenal' THEN FTAG - FTHG
ELSE 0
END AS GD
FROM [Games].[dbo].[Master]
WHERE (HomeTeam = 'Arsenal' OR AwayTeam = 'Arsenal') AND Date < '20121201'
ORDER BY Date DESC
所以像輸出作爲正常的,但在底部一排,與HomeTeam =「總計」號,點=(點列的總和),GD =(GD列的總和)。
Date HomeTeam AwayTeam FTHG FTAG FTR Points GD
----------------------- --------------- ----------- ---- ---- --- ------ --
2012-11-28 00:00:00.000 Everton Arsenal 1 1 D 1 0
2012-11-24 00:00:00.000 Aston Villa Arsenal 0 0 D 1 0
2012-11-17 00:00:00.000 Arsenal Tottenham 5 2 H 3 3
2012-11-10 00:00:00.000 Arsenal Fulham 3 3 D 1 0
2012-11-03 00:00:00.000 Man Utd Arsenal 2 1 H 0 -1
2012-10-27 00:00:00.000 Arsenal QPR 1 0 H 3 1
2012-10-20 00:00:00.000 Norwich City Arsenal 1 0 H 0 -1
2012-10-06 00:00:00.000 West Ham United Arsenal 1 3 A 3 2
2012-09-29 00:00:00.000 Arsenal Chelsea 1 2 A 0 -1
2012-09-23 00:00:00.000 Manchester City Arsenal 1 1 D 1 0
2012-09-15 00:00:00.000 Arsenal Southampton 6 1 H 3 5
2012-09-02 00:00:00.000 Liverpool Arsenal 0 2 A 3 2
2012-08-26 00:00:00.000 Stoke City Arsenal 0 0 D 1 0
2012-08-18 00:00:00.000 Arsenal Sunderland 0 0 D 1 0
這可能嗎?
bluefeet救援!!!!!!!!!!! – 2013-03-06 20:28:55
您可以將兩個額外的列添加到輸出中,並隨着'SUM(點)OVER'和'SUM(GD)OVER'一起繼續運行總計。或者,如果你真的想,把查詢放入一個CTE,然後查詢你的CTE UNION查詢你想從CTE得到的總和。 – Glenn 2013-03-06 20:31:33
好的,所以我在我的select語句的末尾添加了'SUM(Points)OVER()AS pSum',但它說Points是一個無效的列名,可能是因爲它是在select語句中創建的,有沒有辦法在這附近? – user2096512 2013-03-06 21:00:22