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

SQL Oracle-optællingsklynger

Ved at bruge LEAD- og LAG-funktioner i ORACLE kan du bygge disse forespørgsler:

1. Antal nedlukninger:

WITH IntTable AS
( SELECT * FROM
  (
   SELECT dt b_date,value,LEAD(dt) OVER (ORDER BY dt) e_date  FROM
     (
        select "Date" dt,"Value" value,
               LAG("Value") OVER (ORDER BY "Date") pvalue,
               LEAD("Value") OVER (ORDER BY "Date") nvalue
        from T
     ) T1
     WHERE pvalue is NULL or value<>pvalue or nvalue is NULL 
   )
WHERE E_DATE is NOT NULL
)
SELECT COUNT(*) FROM IntTable where value = 0 

SQLFiddle-demo

2.Periode mellem hver nedlukning

WITH IntTable AS
( SELECT * FROM
  (
   SELECT dt b_date,value,LEAD(dt) OVER (ORDER BY dt) e_date  FROM
     (
        select "Date" dt,"Value" value,
               LAG("Value") OVER (ORDER BY "Date") pvalue,
               LEAD("Value") OVER (ORDER BY "Date") nvalue
        from T
     ) T1
     WHERE pvalue is NULL or value<>pvalue or nvalue is NULL 
   )
WHERE E_DATE is NOT NULL
)
SELECT b_date,e_date, (e_date-b_date) * 60 * 24 FROM IntTable where value = 1 

SQLFiddle-demo



  1. Sådan indstilles standardbrugeradgangskoden i PostgreSQL

  2. Unik nøgle i Oracle med eksempler

  3. Skift primær nøgle

  4. VBA-kode for at tilføje sammenkædet tabel med primærnøgle