Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello-

I have two temp tables. The first table called ##temp_first has all the id numbers I need to provide for a report. I want to excluded certain id numbers if a certain criteria is met. The problem I am having is its deleting id numbers that I need to keep. For example, I need to delete id numbers if they have a reply entry as 'BACK' but not if the id number also has a 'DONE' associated with it. In the example below, I need to delete id numbers = 3285647550 but NOT delete id number = 3285653374. 3285653374 has both a 'DONE' and 'BACK' associated with it but since it has a BACK associated with it, I'd like to ignore that one.

-- starts with these --

id reply
3285647550 UP
3285647550 BACK

3285653374 UP
3285653374 UP
3285653374 UP
3285653374 DONE
3285653374 BACK

3286680422 DONE
3286688357 DONE
3286688452 DONE
3286696637 DONE


-- need only these --

id reply
3285653374 UP
3285653374 UP
3285653374 UP
3285653374 DONE
3285653374 BACK

3286680422 DONE
3286688357 DONE
3286688452 DONE
3286696637 DONE

What I have tried:

This is what I tried.

DELETE FROM ##temp_final
WHERE id IN (SELECT id FROM ##temp_first WHERE reply IN 'BACK')

DELETE FROM ##temp_final
WHERE id IN (SELECT id FROM ##temp_first WHERE (reply IN 'BACK' AND reply NOT IN 'DONE'))

I've tried a few other combinations but I still can't get my desired outcome.
Posted

1 solution

SQL supports boolean operators: AND, OR, NOT - but your first query stands alone and deletes all the data that has BACK - so by the time you reach the second DELETE command, all such rows have been removed so no action is taken.

Remove the first command completely, and the second should work.
 
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