我有一個相當大的查詢被用來獲得一堆結果,而且我幾乎可以肯定這不是做到這一點的方法。它很骯髒。這是可惡的。首先,讓我解釋一下我所希望的表結構:Oracle SQL:簡化我看似可怕的子查詢集合
+------------------+------+------+------+------+--------+
| CURRENT_DATE | RO | FL | LM | AO | TOTAL |
+------------------+------+------+------+------+--------+
| 1/2/2012 | 31 | 33 | 70 | 10 | 144 |
+------------------+------+------+------+------+--------+
以上數據集從下表收集:
+---------------+--------------------+
| CURRENT_DATE | PORTABLE_PEANUTS |
+---------------+--------------------+
| 1/2/2012 | RO |
+---------------+--------------------+
| 2/4/2013 | FL |
+---------------+--------------------+
| 3/6/2014 | LM |
+---------------+--------------------+
| 4/8/2015 | AO |
+---------------+--------------------+
從本質上講,我試圖收集是發生在PORTABLE_PEANUTS
上有一定的一切日期,發生的頻率以及發生了什麼。
下面是我使用的查詢:
SELECT total.CURRENT_DATE, results.RO, results.FL, results.LM, results.AO, total.TOTAL FROM
(
SELECT CURRENT_DATE, SUM(RO+FL+LM+AO) TOTAL FROM
(
SELECT a.CURRENT_DATE, a.RO, b.FL, c.LM, d.AO FROM
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) RO FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'RO'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) a
JOIN
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) FL FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'FL'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) b ON a.CURRENT_DATE = b.CURRENT_DATE
JOIN
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) LM FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'LM'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) c ON a.CURRENT_DATE = c.CURRENT_DATE
JOIN
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) AO FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'AO'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) d ON a.CURRENT_DATE = d.CURRENT_DATE
)
GROUP BY CURRENT_DATE
) total
JOIN
(
SELECT a.CURRENT_DATE, a.RO, b.FL, c.LM, d.AO FROM
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) RO FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'RO'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) a
JOIN
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) FL FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'FL'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) b ON a.CURRENT_DATE = b.CURRENT_DATE
JOIN
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) LM FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'LM'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) c ON a.CURRENT_DATE = c.CURRENT_DATE
JOIN
(
SELECT TO_CHAR(SESSION_DATE, 'yyyy-mm-dd') CURRENT_DATE, COUNT(PORTABLE_PEANUTS) AO FROM CORE.DATE_TEST
WHERE PORTABLE_PEANUTS LIKE 'AO'
GROUP BY TO_CHAR(SESSION_DATE, 'yyyy-mm-dd')
) d ON a.CURRENT_DATE = d.CURRENT_DATE
) results ON total.CURRENT_DATE = results.CURRENT_DATE
ORDER BY CURRENT_DATE ASC;
現在這個查詢工作,並且相對來說,它的速度不夠快,但它看起來醜陋。這看起來很難維護,我很確定我在這裏錯過了一些東西。
哇,那真的很棒。有條件的聚合 - 我必須記住這一點。它不僅能夠以更少的步驟完成我想要的操作,而且它的速度提高了一百倍。 –