Click here to Skip to main content
16,022,205 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

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
 
First, you have to create a list of IDs you don't want to delete:

SELECT id
FROM  ##temp_final A
WHERE reply = 'BACK' AND 
NOT EXISTS (SELECT 'X' FROM ##temp_final B
            WHERE reply = 'DONE' AND A.id = B.id)


Then you put this in your DELETE query:

DELETE FROM ##temp_final
WHERE id IN (SELECT id
             FROM  ##temp_final A
             WHERE reply = 'BACK' AND 
             NOT EXISTS (SELECT 'X' FROM ##temp_final B
                         WHERE reply = 'DONE' AND A.id = B.id));
 
Share this answer
 
I have tried this one. It will work for your confusions.

SQL
-- Step 1: Identify the IDs that should be kept (those with 'DONE')
WITH CTE_KeepIDs AS (
    SELECT id
    FROM ##temp_first
    WHERE reply = 'DONE'
)

-- Step 2: Delete rows with 'BACK' reply where the ID does not have a 'DONE' reply
DELETE FROM ##temp_first
WHERE reply = 'BACK'
AND id NOT IN (SELECT id FROM CTE_KeepIDs);

-- Step 3: Delete any other rows with the same ID that only had 'BACK' reply
DELETE FROM ##temp_first
WHERE id NOT IN (SELECT DISTINCT id FROM ##temp_first WHERE reply = 'DONE');

-- Step 4: Select the remaining rows
SELECT *
FROM ##temp_first
ORDER BY id;
 
Share this answer
 
To solve this issue, you need to delete the IDs that have the entry 'BACK' only if they do not also have an entry 'DONE'. Here's an SQL query that will accomplish this:
DELETE FROM ##temp_first
WHERE id_number IN (
SELECT id_number
FROM ##temp_first
WHERE reply_entry = 'BACK'
)
AND id_number NOT IN (
SELECT id_number
FROM ##temp_first
WHERE reply_entry = 'DONE'
);
Explanation:
First Subquery: The first subquery selects id_numbers that have a reply_entry of 'BACK'.
Second Subquery: The second subquery selects id_numbers that have a reply_entry of 'DONE'.
Delete Condition: The main query deletes from ##temp_first only the IDs that are in the first subquery but not in the second. This way, if an id_number has both 'BACK' and 'DONE', it will not be deleted.
This should solve your problem and ensure that only the IDs with 'BACK' and without 'DONE' are removed, I have done a related solution for webzeto.com You can compare as well.
 
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