MW211 EXIT

devlog
MSSQL/ピボット表
2015年07月10日
こんな感じ。
┌──────────────────────────────────────┐
│SELECT ISNULL(CONVERT(varchar, [群]), '計') AS [群],                        │
│       ISNULL([縦]                  , '計') AS [縦],                        │
│       SUM([横1])   AS [横1],                                             │
│       SUM([横2])   AS [横2],                                             │
│       SUM([横3])   AS [横3],                                             │
│       SUM([計])  AS [計]                                                   │
│    FROM (                                                                  │
│        SELECT [群],                                                        │
│               [縦],                                                        │
│               [横1],                                                      │
│               [横2],                                                      │
│               [横3],                                                      │
│               ([横1] + [横2] + [横3]) AS [計]                           │
│            FROM [表]                                                       │
│                PIVOT (COUNT([ID])                                          │
│                           FOR [区分]                                       │
│                           IN ([横1],[横2],[横3])) AS [ピボット]         │
│        UNION ALL                                                           │
│        SELECT [群D].[群],                                                 │
│               [縦D].[縦],                                                 │
│               NULL                  AS [横1],                             │
│               NULL                  AS [横2],                             │
│               NULL                  AS [横3],                             │
│               NULL                  AS [計]                                │
│            FROM (                                                          │
│                SELECT DISTINCT [群]                                        │
│                    FROM [表]                                               │
│            ) AS [群D]                                                     │
│                CROSS JOIN (                                                │
│                    SELECT DISTINCT [縦]                                    │
│                        FROM [表]                                           │
│                ) AS [縦D]                                                 │
│    ) AS [ピボットD]                                                       │
│    GROUP BY GROUPING SETS (                                                │
│                 (),                                                        │
│                 ([縦]),                                                    │
│                 ([群]),                                                    │
│                 ([群],[縦])                                                │
│             )                                                              │
│    ORDER BY [群] ASC,                                                      │
│             [縦] ASC;                                                      │
└──────────────────────────────────────┘
こんな結果になる(はず)。
┌──┬──┬──┬──┬──┬──┐
│ 群 │ 縦 │横1│横2│横3│ 計 │
├──┼──┼──┼──┼──┼──┤
│    │    │    │    │    │    │
│    ├──┼──┼──┼──┼──┤
│    │    │    │    │    │    │
│    ├──┼──┼──┼──┼──┤
│    │ 計 │    │    │    │    │
├──┼──┼──┼──┼──┼──┤
│ 計 │ 計 │    │    │    │    │
└──┴──┴──┴──┴──┴──┘
分類:MSSQL