Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
madhulatha_b
Starting Member
22 Posts |
Posted - 2009-03-24 : 21:10:08
|
| Hi,I have a table which contains the data as belowAccountID Type Label precedingAccountid 20002 331 T1 1110620005 331 T2 2000220003 331 T3 -1110620004 331 T4 -2000320006 331 T5 -2000420008 331 T6 -2000620007 331 T7 2000420011 331 T8 -20005 I have an account with accountid 11106. From Application, if I insert an account before the accountid 11106, that new account will have a record with precedingaccountid -11106 and if I add new account after the accountid 11106, it will have a record with precedingaccountid 11106. The cycle goes on.Now I need to delete all the records from this table which belongs to accountid 11106. In that case I need to delete all the above records. I cannot fetch the records based on TypeId, because the typeid may have multiple accountids.Can anybody help me to write query to delete the records. |
|
|
chrianth
Yak Posting Veteran
50 Posts |
Posted - 2009-03-25 : 02:12:53
|
| Hi,I haven't tried this query but i think it would work...I'm not sure if there would be a more better or shorter query that could have your expected data...--Step 1, get those with positive valuesselect AccountID, [Type], Label,precedingAccountid into #temp_posfrom your_tablewhere precedingAccountid not like '-%'--Step 2, get those with negative valuesselect AccountID, [Type], Label,((precedingAccountid) * (-1)) precedingAccountid into #temp_negfrom your_tablewhere precedingAccountid like '-%'--Step 3, get those positive values that have corresponding negative valueselect a.AccountID, [a.Type], a.Labelinto #for_delete_posfrom #temp_pos ajoin #temp_neg bon(a.precedingAccountid = b.precedingAccountid)--Step 4, get those negative values that have corresponding positive valueselect b.AccountID, [b.Type], b.Labelinto #for_delete_negfrom #temp_pos ajoin #temp_neg bon(a.precedingAccountid = b.precedingAccountid)--Step 5, delete the records on the main table based on the keys valuesdelete from your_tablewhere ((cast(AccountID as varchar)+cast([Type] as varchar)+cast(Label as varchar))in (select (cast(AccountID as varchar)+cast([Type] as varchar)+cast(Label as varchar)) from #for_delete_neg))or ((cast(AccountID as varchar)+cast([Type] as varchar)+cast(Label as varchar))in (select (cast(AccountID as varchar)+cast([Type] as varchar)+cast(Label as varchar)) from #for_delete_pos)) |
 |
|
|
madhulatha_b
Starting Member
22 Posts |
Posted - 2009-03-25 : 18:31:52
|
| Thank you. I will check this. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-28 : 04:39:39
|
i think what you want is a CTE;WIth CTE(AccountID, Type, Label, precedingAccountid )AS(SELECT AccountID, Type, Label, precedingAccountid FROM TableWHERE ABS(precedingAccountid)= 11106UNION ALLSELECT t.AccountID, t.Type, t.Label, t.precedingAccountid FROM CTE cJOIN Table tON t.precedingAccountid = c.AccountID )DELETE tFROM YourTable tJOIN CTE cON c.AccountID=t.AccountIDOPTION (MAXRECURSION 0) |
 |
|
|
|
|
|
|
|