Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Finden einer Reihe von Zeilen aus einer geordneten Ergebnismenge

Sie benötigen mehrere verschachtelte Fensterfunktionen:

SELECT *
FROM
 (
   SELECT the_date, name, grp,
      COUNT(*) OVER (PARTITION BY grp) AS cnt
   FROM
    (
      SELECT the_date, name, 
         SUM(flag) OVER (ORDER BY the_date) AS grp
      FROM
       (
         SELECT the_date, name, 
            CASE WHEN LAG(name) OVER (ORDER BY the_date) = name THEN 0 ELSE 1 END AS flag
         FROM orders
         WHERE 
             the_date BETWEEN 
                 TO_DATE('2013-09-18',..) AND 
                 TO_DATE('2013-09-22', ..)
       ) dt
    ) dt
 ) dt
WHERE cnt >= 3
ORDER BY the_date