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

Menghapus jam tumpang tindih dari hitungan hari tidak bekerja

Pengaturan Oracle :

CREATE TABLE Trip_Dates ( Start_Date, End_Date ) AS
SELECT TO_DATE( '2016-02-01 04:00:00', 'YYYY-MM-DD HH24:MI:SS' ), TO_DATE( '2016-02-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL UNION ALL
SELECT TO_DATE( '2016-02-02 14:00:00', 'YYYY-MM-DD HH24:MI:SS' ), TO_DATE( '2016-02-06 20:00:00', 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL UNION ALL
SELECT TO_DATE( '2016-02-01 01:00:00', 'YYYY-MM-DD HH24:MI:SS' ), TO_DATE( '2016-02-02 07:00:00', 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL;

Kueri :

WITH Dates ( dt, start_end ) AS (
  SELECT GREATEST( start_date, TRUNC( SYSDATE, 'MM' ) ),
         1
  FROM   trip_dates
  WHERE  start_date < ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 )
  AND    end_date > TRUNC( SYSDATE, 'MM' )
  UNION ALL
  SELECT LEAST( end_date, ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 ) ),
         -1
  FROM trip_dates
  WHERE  start_date < ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 )
  AND    end_date > TRUNC( SYSDATE, 'MM' )
  ORDER BY 1, 2
)
,range_start_ends ( dt, start_end, range_start, range_end ) AS (
  SELECT d.*,
         CASE
           WHEN start_end =  1
            AND SUM( start_end ) OVER ( ORDER BY dt ) = 1
           THEN dt
           ELSE NULL
            END,
         CASE
           WHEN start_end = -1
            AND SUM( start_end ) OVER ( ORDER BY dt ) = 0
           THEN dt
           ELSE NULL
            END
  FROM   Dates d
),
worked_days ( worked_days ) AS (
  SELECT range_end - LAG( range_start ) IGNORE NULLS OVER ( ORDER BY dt )
  FROM   range_start_ends
)
SELECT ( ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 )
        - TRUNC( SYSDATE, 'MM' ) -
        SUM( worked_days )
       ) * 24 AS unworked_hours
FROM   worked_days;

Hasil :

UNWORKED_HOURS
--------------
           561 



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Tidak dapat mengambil id dari baris yang dimasukkan terakhir di Hibernate menggunakan Oracle

  2. Data Kueri dari XML

  3. Mendapatkan jumlah beberapa kolom dari dua tabel

  4. cara mengonversi string tanggal ke format tanggal di oracle10g

  5. Apakah mungkin untuk menyimpan gambar di oracle 11g express edition?