2013-03-06 183 views
2

我想使用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 

這可能嗎?

+1

bluefeet救援!!!!!!!!!!! – 2013-03-06 20:28:55

+1

您可以將兩個額外的列添加到輸出中,並隨着'SUM(點)OVER'和'SUM(GD)OVER'一起繼續運行總計。或者,如果你真的想,把查詢放入一個CTE,然後查詢你的CTE UNION查詢你想從CTE得到的總和。 – Glenn 2013-03-06 20:31:33

+0

好的,所以我在我的select語句的末尾添加了'SUM(Points)OVER()AS pSum',但它說Points是一個無效的列名,可能是因爲它是在select語句中創建的,有沒有辦法在這附近? – user2096512 2013-03-06 21:00:22

回答

2

你可以使用CROSS APPLY計算PointsGD。這樣,你就能夠引用產生相同的選擇:

SELECT 
    m.Date, 
    m.HomeTeam, 
    m.AwayTeam, 
    m.FTHG, 
    m.FTAG, 
    m.FTR, 
    x.Points, 
    x.GD, 
    TotalPoints = SUM(x.Points) OVER(), 
    TotalGD  = SUM(x.GD) OVER() 
FROM [Games].[dbo].[Master] AS m 
CROSS APPLY (
    SELECT 
    Points = CASE 
     WHEN m.FTR = 'D' THEN 1 
     WHEN m.FTR = 'H' AND m.HomeTeam = 'Arsenal' 
     OR m.FTR = 'A' AND m.AwayTeam = 'Arsenal' THEN 3 
     ELSE 0 
    END, 
    GD = CASE m.HomeTeam 
     WHEN 'Arsenal' 
     THEN m.FTHG - m.FTAG 
     ELSE m.FTAG - m.FTHG 
    END 
) AS x 
WHERE (m.HomeTeam = 'Arsenal' OR m.AwayTeam = 'Arsenal') AND m.Date < '20121201' 
ORDER BY m.Date DESC 
; 
+0

感謝您提供的所有答案,我已經使用這個答案,完美地工作。乾杯。 – user2096512 2013-03-07 14:16:25

2

你沒有說明你使用的是什麼版本的SQL Server,但如果你正在使用SQL Server 2008+,那麼你應該能夠做到這一點使用GROUPING SETS得到捲起行:

;with cte as 
(
    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' 
) 
select 
    coalesce(convert(char(10), date, 120), 'Total')date, 
    coalesce(hometeam, '') hometeam, 
    coalesce(awayteam , '') awayteam, 
    sum(fthg) fthg, 
    sum(ftag) ftag, 
    coalesce(ftr, '') ftr, 
    sum(points) points, 
    sum(gd) gd 
from cte 
group by grouping sets((date, hometeam, awayteam, ftr),()) 

請參閱SQL Fiddle with Demo

如果您正在運行SQL Server 2005+,那麼你也可以使用ROLLUP()

;with cte as 
(
    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 
    WHERE (HomeTeam = 'Arsenal' OR AwayTeam = 'Arsenal') AND Date < '20121201' 
) 
select 
    coalesce(convert(char(10), date, 120), 'Total')date, 
    coalesce(hometeam, '') hometeam, 
    coalesce(awayteam , '') awayteam, 
    sum(fthg) fthg, 
    sum(ftag) ftag, 
    coalesce(ftr, '') ftr, 
    sum(points) points, 
    sum(gd) gd 
from cte 
group by rollup((date, hometeam, awayteam, ftr)) 

SQL Fiddle with Demo

+1

您可以用'rollup((date,hometeam,awayteam,ftr))'替換'(日期,hometeam,awayteam,ftr),())',這將使查詢在SQL Server 2005中可運行。 – 2013-03-07 00:37:07

+0

@AndriyM我剛剛在該版本上發佈。 :) – Taryn 2013-03-07 00:37:35

+0

啊,對我的急躁抱歉。 :) – 2013-03-07 00:38:29