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....

假设我要在 eventval 列中添加另一个值,例如 TKQTY,我将不得不使用另一个 UNION 语句,我不想​​这样做。你能帮我找到另一个解决方案吗?我到处搜索,但找不到答案。请帮忙。多谢。

4

1 回答 1

0

您的查询太长。如果我的理解正确,您可以在子查询中放置“联合”。像这样:

SELECT EVENTVAL, EQPID,CASE WHEN..,CASE WHEN..
FROM (
SELECT CUTOFF_DATE, EQPID, SUM(TKOUTQTY) QTYO,'TKOUTQTY' AS EVENTVAL
        FROM DAILY_DATA
        WHERE CUTOFF_DATE BETWEEN '20120401' AND '20120530'
        AND EQPID LIKE 'MS-%'
        GROUP BY CUTOFF_DATE, EQPID
UNION
SELECT CUTOFF_DATE, EQPID, SUM(TKINQTY) QTYO,'TKINQTY' AS EVENTVAL
        FROM DAILY_DATA
        WHERE CUTOFF_DATE BETWEEN '20120401' AND '20120530'
        AND EQPID LIKE 'MS-%'
        GROUP BY CUTOFF_DATE, EQPID
UNION
..)
GROUP BY EVENTVAL, EQPID
于 2013-05-09T03:01:40.600 回答