I should delete duplicate rows from my table. My table structure is : ClientId-GiftId-Invoice-Quantite
Example of Duplicate Rows :
Example 1:
1. C1-G1-Inv1-1
2. C1-G1-Inv1-0
3. C1-G1-NULL-NULL
Expected Result : 2 AND 3 should be deleted
Example 2:
1. C2-G1-NULL-NULL
2. C2-G1-NULL-NULL
3. C2-G1-NULL-NULL
Expected Result : any row is accepted
My delete query is :
WITH CTE AS(
SELECT ClientID,GiftID,Invoice,Quantity,
RN = ROW_NUMBER()OVER(PARTITION BY ClientID,GiftID,Invoice,Quantity ORDER BY
ClientID,GiftID,Invoice)
FROM #RowsToDelete
)
Delete FROM CTE where RN > 1
i'm new with use of ROW_NUMBER() Function and i think i didn't figure how to use it properly Thanks in advance
PARTITION BYdoes, as you appear to be partitioning, and ordering on every columns apart fromquantity.The columns in thePARTITION BYdon't need to be in theORDER BY. ThePARTITION BYis the distinct groups you want to create, and theORDER BYthe order you want those distinct groups in.