Monday, August 10, 2026

173 ) 14 ) Query to find duplicate rows of loan id but time is different

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

14 )  Query to find duplicate rows of loan id but time is different 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Due to a bug in an upstream system, a source file sent duplicate records for the same loan account number on the same day. 

Each duplicate row has a different SnapshotTimestamp. 

How would you write a query to select only the most recent row from all duplicate  loans account and filter out the older duplicates?

-----

WITH RankedLoans AS (
    SELECT 
        LoanNumber,
        Timestamp,
        ROW_NUMBER() OVER (
            PARTITION BY LoanNumber 
            ORDER BY creationdate DESC
        ) AS rn
    FROM Loans
)
SELECT 
    LoanNumber,
    creationdate  
FROM RankedLoans WHERE rn = 1;

------------------------------------------------------------------------------

SELECT
LoanNumber,
Timestamp
FROM RankedLoans WHERE rnk = 1;



---------------

No comments:

Post a Comment

239 ) Metadata Management

Metadata Management and Modern Data Governance Tools Metadata management forms the backbone of data governance, data lineage, and data quali...