Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table with 5 columns

IDCPT4CPT4ModItemChargeEff_Date


Deletion Condition:

When rows exists with duplicate CPT4 and CPT4Mod field values but different ItemCharge and Eff_Date values, exclude all but the most recent Eff_Date records.

I need a query which can achieve this in two scenarios.

1. When CPT4Mod is given by the user.
2. When CPT4Mod is not given by the user.

Later I need to achieve the same using LINQ.
Posted
Updated 21-Dec-14 16:37pm
v5
Comments
Maciej Los 21-Dec-14 13:34pm    
More details needed. Post sample data and expected result.
[EDIT]
Unclear... still
ArunRajendra 21-Dec-14 23:02pm    
Post what have you tried?
Sinisa Hajnal 22-Dec-14 3:24am    
I don't know LINQ enough for this. But in SQL version. Create a table with the dates you have to retain (use MAX (Eff_Date) - most recent). Then use minus operator to delete everything except those you already selected.
Sinisa Hajnal 22-Dec-14 6:26am    
I found LINQ solution (three different ways) easily - you didn't do anything to look for this before asking. Vote 1.
Praveen Kumar Upadhyay 22-Dec-14 7:09am    
Delete Condition is not clear. Please post some sample data and expected output.

SQL
SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY CPT4, CPT4MOD, ITEMCHARGE ORDER BY EFF_DATE DESC) AS RowNo
    , *
FROM #table
) AS T
WHERE T.RowNo = 1
 
Share this answer
 
Comments
PIEBALDconsult 22-Dec-14 10:00am    
Don't answer your own question. Just use Improve question to add it to the question.
SQL
WITH CTE AS(
       SELECT COL1, COL2, COL3,COL4,
RN = ROW_NUMBER()OVER(PARTITION BY COL1 ORDER BY COL1)
   FROM TABLE NAME
)
DELETE FROM CTE WHERE RN > 1
 
Share this answer
 
Just on the fly and of course not tested and most probably performance problems:

SQL
DELETE FROM MyTable MyTable_Delete
WHERE EXISTS (SELECT MyTable_Cond.ID FROM MyTable MyTable_Cond
               WHERE MyTable_Cond.CPT4 = MyTable_Delete.CPT4
                 AND MyTable_Cond.CPT4Mod = MyTable_Delete.CPT4Mod
                 AND MyTable_Cond.ItemCharge <> MyTable_Delete.ItemCharge
                 AND MyTable_Cond.Eff_Date <> MyTable_Delete.Eff_Date)
            AND MyTable_Delete.Eff_Date < (SELECT MAX(MyTable_Latest.Eff_Date)
                                             FROM MyTable MyTable_Latest
                                             WHERE MyTable_Latest.CPT4 = MyTable_Delete.CPT4
                                             AND MyTable_Latest.CPT4Mod = MyTable_Delete.CPT4Mod
                                             AND MyTable_Latest.ItemCharge <> MyTable_Delete.ItemCharge
                                             AND MyTable_Latest.Eff_Date <> MyTable_Delete.Eff_Date)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900