SQL:Rollup没有分组

时间:2013-03-06 20:21:37

标签: sql rollup

我正在尝试使用ROLLUP将输出底部的小计添加到此代码中,但它无法说明它们不在组中。我不希望他们分组,我只想在底部小计。

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

所以id像输出一样正常,但底部有一行,HomeTeam =“Total”,Points =(点数列的总和),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

有可能吗?

2 个答案:

答案 0 :(得分:3)

您可以使用CROSS APPLY来计算PointsGD。这样你就可以在同一个SELECT中引用结果:

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
;

答案 1 :(得分: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