I need to do a add a column (CTRL) to verify that in each partition (ID) the sequence of the dates (REFERENCE_DATE) is correct keeping in consideration the column NUM_ROLL;
The db is ORACLE and I am using SQL DEVELOPER The data is ordered By ID, NUM_ROLL, REFERENCE_DATE
current table with the new column that I have to create
I expect to have the new column CTRL having values 0 or 1
If, for each PARTITION ID
, you want to order the results by NUM_ROLL
and then REFERENCE_DATE
and check whether the previous REFERENCE_DATE
is less than the current REFERENCE_DATE
then you can use the LAG
analytic function and a CASE
expression:
SELECT t.*,
CASE
WHEN LAG(reference_date) OVER (
PARTITION BY partition_id
ORDER BY num_roll, reference_date
) >= reference_date
THEN 1
ELSE 0
END AS ctrl
FROM table_name t
Which, for the sample data:
CREATE TABLE table_name (partition_id, num_roll, reference_date) AS
SELECT 'A', 1, DATE '2024-07-15' FROM DUAL UNION ALL
SELECT 'A', 2, DATE '2024-07-16' FROM DUAL UNION ALL
SELECT 'A', 2, DATE '2024-07-14' FROM DUAL UNION ALL
SELECT 'A', 2, DATE '2024-07-17' FROM DUAL UNION ALL
SELECT 'B', 2, DATE '2024-06-04' FROM DUAL UNION ALL
SELECT 'B', 2, DATE '2024-06-05' FROM DUAL UNION ALL
SELECT 'B', 3, DATE '2024-06-06' FROM DUAL UNION ALL
SELECT 'B', 4, DATE '2024-06-07' FROM DUAL UNION ALL
SELECT 'B', 5, DATE '2024-06-10' FROM DUAL;
Outputs:
PARTITION_ID | NUM_ROLL | REFERENCE_DATE | CTRL |
---|---|---|---|
A | 1 | 2024-07-15 00:00:00 | 0 |
A | 2 | 2024-07-14 00:00:00 | 1 |
A | 2 | 2024-07-16 00:00:00 | 0 |
A | 2 | 2024-07-17 00:00:00 | 0 |
B | 2 | 2024-06-04 00:00:00 | 0 |
B | 2 | 2024-06-05 00:00:00 | 0 |
B | 3 | 2024-06-06 00:00:00 | 0 |
B | 4 | 2024-06-07 00:00:00 | 0 |
B | 5 | 2024-06-10 00:00:00 | 0 |
Note: If you do not want to order the results by NUM_ROLL, REFERENCE_DATE
and want to use a different ordering then you will need to provide a set of columns that will generate your desired ordering because without something to order the rows by then the result set will be non-deterministically ordered (i.e. randomly in the order that the rows are retrieved).