2

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

出来ますか?

4

2 に答える 2

3

CROSS APPLYを計算するために使用できます。そうすれば、同じ SELECT で結果を参照できます。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
;
于 2013-03-07T00:31:50.283 に答える
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 を参照してください

于 2013-03-07T00:27:59.683 に答える