Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

SQL Oracle Menghitung Cluster

Dengan menggunakan fungsi LEAD dan LAG di ORACLE, Anda dapat membuat kueri berikut:

1.Jumlah shutdown:

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 

Demo SQLFiddle

2.Periode Antara setiap shutdown

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 

Demo SQLFiddle



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Redaksi Data 12c

  2. Alat untuk bekerja dengan prosedur tersimpan di Oracle, dalam tim?

  3. ORA-01008:tidak semua variabel terikat. Mereka terikat

  4. Bagaimana cara memeriksa apakah nilai DataReader bukan nol?

  5. Menulis di ExcelSheet menggunakan paket UTL_FILE di Oracle