1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| WITH t1 AS
(
SELECT '101' AS my_id, SYSDATE-254 AS my_date FROM dual UNION ALL
SELECT '101' AS my_id, SYSDATE-25 AS my_date FROM dual UNION ALL
SELECT '101' AS my_id, SYSDATE+453 AS my_date FROM dual UNION ALL
SELECT '102' AS my_id, SYSDATE AS my_date FROM dual UNION ALL
SELECT '101' AS my_id, SYSDATE+150 AS my_date FROM dual
)
SELECT t1.my_id,
COUNT(*)
FROM t1 ,
(SELECT my_id,
MIN(my_date) min_date
FROM t1 t2
GROUP BY my_id) tmp_min_date
WHERE t1.my_id = tmp_min_date.my_id
AND t1.my_date < add_months(tmp_min_date.min_date, 12)
GROUP BY t1.my_id |
Partager