Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove oldest record in SQL table when there is more than 1 record with the same name

I’ve a Pipeline in Synapse which syncs data from a source SQL database to a destination SQL database. As soon as the copy data activity is done it inserts a record (with a stored procedure) in a log table.

This table is used to sync data incremental. As soon as I start my PipeLine again it will compare the date in this log table with the DateChanged date of the source table so that it knows which records to sync.

At the end of the PipeLine run I would like to delete the record with the oldest TransferTime. So that I keep my table clean and small. But I only want to do this when there are more than 1 record for the table. Just to prevent that my whole pipeline fails because it can’t find a last TransferTime, so it doesn’t know which records are changed synce last run.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In the example below I want to delete the oldest records of Table A and Table B which have the TransferTime 2023-03-28 10:00.

TableName TransferTime
Table A 2023-03-28 10:00
Table A 2023-03-28 10:15
Table B 2023-03-28 10:00
Table B 2023-03-28 10:15
Table C 2023-03-28 10:00

>Solution :

Exists provides one canonical way to do this. Given that you are using SQL Server, you could also use a deletable CTE here:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY TableName
                                 ORDER BY TransferTime DESC) rn
    FROM yourTable
)

DELETE
FROM cte
WHERE rn > 1;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading