1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| WITH tvente AS
(SELECT TO_DATE('02/07/2007', 'DD/MM/RRRR') AS dte,
4 AS qte, 10 AS px FROM dual
UNION ALL
SELECT TO_DATE('05/07/2007', 'DD/MM/RRRR'), 6, 10 FROM dual
UNION ALL
SELECT TO_DATE('14/07/2007', 'DD/MM/RRRR'), 7, 11 FROM dual
UNION ALL
SELECT TO_DATE('17/07/2007', 'DD/MM/RRRR'), 3, 11 FROM dual
UNION ALL
SELECT TO_DATE('20/07/2007', 'DD/MM/RRRR'), 5, 10 FROM dual
UNION ALL
SELECT TO_DATE('23/07/2007', 'DD/MM/RRRR'), 4, 10 FROM dual
)
SELECT MIN(dte), dte_max, px, SUM(qte)
FROM (
SELECT dte, qte, px, NVL(
(
SELECT MIN(dte) - 1 FROM tvente t2
WHERE t2.px <> t1.px AND t2.dte >= t1.dte
),
(SELECT MAX(dte) FROM tvente t2
WHERE t2.px = t1.px AND t2.dte >= t1.dte
AND NOT EXISTS (SELECT 1 FROM tvente t3 WHERE t3.dte BETWEEN t1.dte AND t2.dte
AND px <> t2.px
)
)
) AS dte_max
FROM tvente t1
ORDER BY dte
)
GROUP BY dte_max, px
MIN(DTE) DTE_MAX PX SUM(QTE)
02/07/2007 13/07/2007 10 10
14/07/2007 19/07/2007 11 10
20/07/2007 23/07/2007 10 9 |
Partager