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

Mengelompokkan Variabel Biner Berdasarkan ID dan Tanggal Min/Maks

Dari Oracle 12, ini adalah jenis masalah yang MATCH_RECOGNIZE dimaksudkan untuk memecahkan:

SELECT *
FROM   table_name
MATCH_RECOGNIZE (
  PARTITION BY Emp_ID
  ORDER     BY dt
  MEASURES
    FIRST(dt) AS min_date,
    LAST(dt)  AS max_date,
    MATCH_NUMBER() AS group_number
  PATTERN (one+)
  DEFINE
    one AS dummy = 1
)

Yang, untuk data sampel:

CREATE TABLE table_name ( dt, Emp_ID, Dummy ) AS
SELECT DATE '2021-01-01', 5, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-02', 5, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-03', 5, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-04', 5, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-05', 5, 0 FROM DUAL UNION ALL
SELECT DATE '2021-01-06', 5, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-07', 5, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-01', 8, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-02', 8, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-03', 8, 1 FROM DUAL UNION ALL
SELECT DATE '2021-01-04', 8, 0 FROM DUAL UNION ALL
SELECT DATE '2021-01-05', 8, 0 FROM DUAL UNION ALL
SELECT DATE '2021-01-06', 8, 0 FROM DUAL UNION ALL
SELECT DATE '2021-01-07', 8, 1 FROM DUAL;

Keluaran:

db<>fiddle di sini




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Pengguna Umum Dengan hak istimewa SYSBACKUP

  2. Bagaimana cara mengisi tabel kalender di Oracle?

  3. meneruskan array ke prosedur tersimpan

  4. kembalikan satu baris dalam prosedur tersimpan di Oracle

  5. Buat skrip SQL*Plus berhenti