SQL_beginner1 (6/12/2013)
Hi,I have a table (t_account_code) with the following three columns - AccountCode, StartDate and EndDate. The table does not have an ID column (I know - it was inherited) and at this point, modifying the table is not an option, so please bear with me. With the combination of three columns, we get a PK.
This table has multiple entries for the same AccountCode, and I want to delete all duplicate/triplicate/multiple entries EXCEPT for the latest one, based on StartDate.
First, I've created a temp table with all account codes (unique) and their count, if greater than 1.
I've tried the following query, first to do a select on the accounts that occur twice:
select * from t_account_code tac1
where AccountCode in (select AccountCode from #Tmp_accountcodes where count_num = 2)
and StartDate = (select MIN(StartDate) from t_account_code tac2 where tac2.AccountCode = tac1.AccountCode)
The distinct values of count_num are 2, 3 and 4.
This works as intended and gives me the results I want (for duplicate values), but when I change it to a delete statement, I get a syntax error in the first row for the table alias - tac1. Of course, if I delete it, then my query won't work as I'm using tac1 in the sub-query.
Any solution to this problem will be appreciated, even if it's a different query than the one above. I want to end up with unique AccountCodes in the table, with the latest StartDate, and delete any multiple ones with the older StartDate.
E.g.
AccountCode ---- StartDate ---- EndDate
1234------------ 12/31/2012 ---- 3/30/2013
1234------------ 3/31/2013 ------4/29/2013
1234------------ 4/30/2013 -----12/31/2014
9876------------12/31/2012------3/30/2013
9876------------3/31/2013 -------5/30/2014
I want to delete the first two rows for AccountCode '1234' and delete the first row for AccountCode '9876.'
If an AccountCode occurs only once, then I want to leave it alone.
Thanks.
Give this a try in a test environment:
with DelDups as (
select
AccountCode,
StartDate,
EndDate,
rn = row_number() over (partition by AccountCode order by StartDate desc)
from
t_account_code
)
delete from DelDups
where rn > 1;
Edit: Fixed the code.