Oracle
 sql >> Database >  >> RDS >> Oracle

Trovare una "serie" di righe da un set di risultati ordinato

Hai bisogno di più funzioni della finestra nidificata:

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