请帮我找到另一个解决方案/代码块,以获得相同的结果但避免使用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语句而我不想这样做。能帮我找到另一个解决方案吗?我搜遍了所有但无法找到答案。请帮忙。非常感谢。
答案 0 :(得分:0)
你的查询太长了..如果我的理解正确,你可以在子查询中放置'union'。 像这样:
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