MW211 EXIT

devlog
Oracle/年月表
2018年02月05日
ピボット集計にて、縦軸に年、横軸に1~12月のマトリクス表を出力するSQL文。
┌──────────────────────────────────────┐
│WITH "年月D" AS (                                                          │
│    SELECT "年",                                                            │
│           "月",                                                            │
│           COUNT(*) AS "値"                                                 │
│        FROM (                                                              │
│            SELECT "年"                                                     │
│                   "月"                                                     │
│                FROM "表"                                                   │
│        )                                                                   │
│        GROUP BY "年",                                                      │
│                 "月"                                                       │
│)                                                                           │
│SELECT *                                                                    │
│    FROM (                                                                  │
│        SELECT "カレンダM"."年",                                           │
│               "カレンダM"."月",                                           │
│               NVL("年月D"."値", 0) AS "値"                                │
│            FROM (                                                          │
│                SELECT "年"."年",                                           │
│                       "月"."月"                                            │
│                    FROM (                                                  │
│                        SELECT 開始年 + ROWNUM - 1  AS "年"                 │
│                            FROM DUAL                                       │
│                                 CONNECT BY ROWNUM <= 終了年 - 開始年 + 1   │
│                    ) "年"                                                  │
│                        CROSS JOIN (                                        │
│                            SELECT ROWNUM AS "月"                           │
│                                FROM DUAL CONNECT BY ROWNUM <= 12           │
│                        ) "月"                                              │
│            ) "カレンダM"                                                  │
│                LEFT JOIN "年月D"                                          │
│                  ON  "年月D"."年" = "カレンダM"."年"                     │
│                  AND "年月D"."月" = "カレンダM"."月"                     │
│    ) "集計D"                                                              │
│    PIVOT (                                                                 │
│        SUM("値") FOR "月" IN ( 1 AS  "1月",                                │
│                                2 AS  "2月",                                │
│                                3 AS  "3月",                                │
│                                4 AS  "4月",                                │
│                                5 AS  "5月",                                │
│                                6 AS  "6月",                                │
│                                7 AS  "7月",                                │
│                                8 AS  "8月",                                │
│                                9 AS  "9月",                                │
│                               10 AS "10月",                                │
│                               11 AS "11月",                                │
│                               12 AS "12月")                                │
│    ) "ピボット"                                                            │
│    ORDER BY "年" ASC;                                                      │
└──────────────────────────────────────┘
  ・「開始年」と「終了年」の部分はそれぞれの数値に置き換える
  ・「年月D」の中身を変えれば、元データをいろいろなものにできる
分類:Oracle