0

同じ結果を得るための別のソリューション/コードブロックを見つけるのを手伝ってください。ただし、UNION は避けてください。

これが私のコードです:

SELECT EQPID, "EVENTVAL" AS "EVENTVAL", SUM("20120501") AS "20120501", SUM("20120502") AS "20120502", SUM("20120503") AS "20120503", SUM("201204") AS "201204", SUM("201205") AS "201205"
FROM
(
SELECT EQPID, "EVENTVAL" AS "EVENTVAL", SUM("20120501") AS "20120501", SUM("20120502") AS "20120502", SUM("20120503") AS "20120503", SUM("201204") AS "201204", SUM("201205") AS "201205"
FROM
(
    SELECT EQPID,
            CASE
            WHEN EQPID LIKE 'MS-%'
            THEN 'TKINQTY'
            END AS "EVENTVAL",

            CASE 
            WHEN CUTOFF_DATE = '20120501' 
            THEN SUM(QTY)
            END AS "20120501",

            CASE
            WHEN CUTOFF_DATE = '20120502'
            THEN SUM(QTY)
            END AS "20120502",

            CASE
            WHEN CUTOFF_DATE = '20120503'
            THEN SUM(QTY)
            END AS "20120503",

            CASE
            WHEN CUTOFF_DATE BETWEEN '20120401' AND '20120430'
            THEN SUM(QTY)
            END AS "201204",

            CASE
            WHEN CUTOFF_DATE BETWEEN '20120501' AND '20120530'
            THEN SUM(QTY)
            END AS "201205"

    FROM
    (
        SELECT CUTOFF_DATE, EQPID, SUM(TKINQTY) QTY
        FROM DAILY_DATA
        WHERE CUTOFF_DATE BETWEEN '20120401' AND '20120530'
        AND EQPID LIKE 'MS-%'
        GROUP BY CUTOFF_DATE, EQPID
        ORDER BY CUTOFF_DATE, EQPID
    )
    GROUP BY EQPID, CUTOFF_DATE
)
GROUP BY EVENTVAL, EQPID
UNION
SELECT EQPID, "EVENTVAL" AS "EVENTVAL", SUM("20120501") AS "20120501", SUM("20120502") AS "20120502", SUM("20120503") AS "20120503",SUM("201204") AS "201204", SUM("201205") AS "201205"
FROM
(
    SELECT EQPID,
            CASE
            WHEN EQPID LIKE 'MS-%'
            THEN 'TKOUTQTY'
            END AS "EVENTVAL",

            CASE 
            WHEN CUTOFF_DATE = '20120501' 
            THEN SUM(QTYO)
            END AS "20120501",

            CASE
            WHEN CUTOFF_DATE = '20120502'
            THEN SUM(QTYO)
            END AS "20120502",

            CASE
            WHEN CUTOFF_DATE = '20120503'
            THEN SUM(QTYO)
            END AS "20120503",

            CASE
            WHEN CUTOFF_DATE BETWEEN '20120401' AND '20120430'
            THEN SUM(QTYO)
            END AS "201204",

            CASE
            WHEN CUTOFF_DATE BETWEEN '20120501' AND '20120530'
            THEN SUM(QTYO)
            END AS "201205"

    FROM
    (
        SELECT CUTOFF_DATE, EQPID, SUM(TKOUTQTY) QTYO
        FROM DAILY_DATA
        WHERE CUTOFF_DATE BETWEEN '20120401' AND '20120530'
        AND EQPID LIKE 'MS-%'
        GROUP BY CUTOFF_DATE, EQPID
        ORDER BY CUTOFF_DATE, EQPID
    )
    GROUP BY EQPID, CUTOFF_DATE
)
GROUP BY EVENTVAL, EQPID
)
GROUP BY ROLLUP(EVENTVAL, EQPID)
ORDER BY EQPID, EVENTVAL

結果:

EQPID | EVENTVAL | 20120501 | 20120502 | 20120503 | 201204 | 201205
____________________________________________________________________

MS-A1 | TKINQTY  | 21528 |   28386 | 18288 | 821295 | 908602

MS-A1 | TKOUTQTY  | 21510 |   28359 | 18720 | 820470 | 907320

MS-B1 | TKINQTY  | 21530 |   28456 | 18852 | 821560 | 908564

AND SO ON....

TKQTY のように、eventval 列に別の値を追加すると仮定すると、別の UNION ステートメントを使用する必要があり、そうしたくありません。これに対する別の解決策を見つけるのを手伝ってもらえますか? あちこち検索しましたが、答えが見つかりません。助けてください。どうもありがとう。

4

1 に答える 1