sqlsnowflake-cloud-data-platform

I want to add a column to a SQL query containing an immediately preceding (by date) value from another column


I have a table of customer records, where each customer can have multiple records, if their old record has been closed (they can only have one "Live" record).

Each record has a created date and a closed date (if it has been closed).

Source table:

customer_id record_id record_status created_date closed_date
1000000 WTLZSMV3RQ Live 15/02/2025 10:36:26
1000001 I8R6FPCOEH Closed 20/09/2024 03:11:22 13/12/2024 06:08:54
1000001 ZRPLYWBR8C Closed 13/12/2024 10:11:10 07/03/2025 09:06:25
1000001 9HJT89GNJ3 Live 07/03/2025 13:08:42
1000002 ER5JF7TZ0Z Live 22/07/2024 22:05:17
1000003 2SNSDMLXZ1 Closed 07/03/2024 13:04:33 03/12/2024 08:34:56
1000003 VDNI7UWAUX Live 03/12/2024 22:19:39

I want a SQL statement which will return the table, with a new column old_record_id.

This will be populated with the record_id for the previous record for that customer. "Previous record" is the record with the closest previous created_date to the created_date of that record. If there are no previous records, it should be null.

Results I want from that SQL query:

customer_id record_id record_status created_date closed_date old_record_id
1000000 WTLZSMV3RQ Live 15/02/2025 10:36:26
1000001 I8R6FPCOEH Closed 20/09/2024 03:11:22 13/12/2024 06:08:54
1000001 ZRPLYWBR8C Closed 13/12/2024 10:11:10 07/03/2025 09:06:25 I8R6FPCOEH
1000001 9HJT89GNJ3 Live 07/03/2025 13:08:42 ZRPLYWBR8C
1000002 ER5JF7TZ0Z Live 22/07/2024 22:05:17
1000003 2SNSDMLXZ1 Closed 07/03/2024 13:04:33 03/12/2024 08:34:56
1000003 VDNI7UWAUX Live 03/12/2024 22:19:39 2SNSDMLXZ1

Solution

  • LAG would work for this (as mentioned in the comments). Here is a snippet:

    SELECT customer_id,
           record_id,
           record_status,
           created_date,
           closed_date,
           LAG (record_id) OVER (PARTITION BY customer_id ORDER BY created_date) as old_record_id
    FROM CUSTOMER_RECORDS
    

    Just a note: this will produce slightly different results than what you've got because for ID = 1000003, the created date (07/03/2025 13:04:33) for the old ID is well before the closed date (03/12/2024 22:19:39).