How to delete multiple columns in MS SQL WHERE IN (SELECT)

I have a table calling it " Cluster " that is linked to the second Element table

Cluster:

 Id Group Ele1 Ele2 Ele3 1 1 1 2 3 2 1 4 NULL 9 3 2 5 8 7 

Element

 Id Name 1 'A' 2 'b' 3 'c' 4 'd' 5 'z' 6 'j' 7 't' 8 'r' 9 'e' 

now i need to remove the cluster

 DELETE FROM Cluster WHERE Cluster.Group= 1 

but before deleting rows from my Cluster I need to delete all related rows from Element

 DELETE FROM Element WHERE Id IN (SELECT Ele1 Ele2 Ele3 FROM Cluster WHERE Cluster.Group= 1) 

but this request does not work, so what did I miss?

+4
source share
7 answers

Compact and clean:

 DELETE FROM Element WHERE Id IN ( SELECT Ele FROM Cluster AS T CROSS APPLY (VALUES (T.Ele1) , (T.Ele2) , (T.Ele3) ) AS X (Ele) WHERE T.Group= 1 ) 
+3
source

Try the following:

 DELETE FROM Element WHERE Id IN (SELECT Ele1 FROM Cluster WHERE Cluster.[Group] = 1 And Ele1 Is Not NULL Union SELECT Ele2 FROM Cluster WHERE Cluster.[Group] = 1 And Ele2 Is Not NULL Union SELECT Ele3 FROM Cluster WHERE Cluster.[Group] = 1 And Ele3 Is Not NULL ) 
+4
source

UPDATED

 delete from Element where ID in ( SELECT Item FROM (SELECT [Group], Ele1, cast([Ele2] as int) Ele2, Ele3 FROM Cluster) p UNPIVOT(Item FOR Ele IN (Ele1, Ele2, Ele3) )AS unpvt ) 
+1
source

There are several working solutions, but I see no explanation why they work, and your attempt does not work.

The IN statement wants to get a list of values โ€‹โ€‹to check. In the simplest case, it looks like a WHERE column IN (value1, value2, value3...) . It also works with SELECT : WHERE column IN (SELECT somecolumn FROM sometable) .

The query does not return a single column that can be used as a list, but three different columns. This is not the correct syntax for the IN statement. G Mastros uses the UNION operator to combine three different select queries into one, one column, the result set.

(In principle, I would use UNION ALL in this case, since duplicate values โ€‹โ€‹are not a problem, and UNION is a slower operation. But check your clientโ€™s statistics, as checking multiple copies of values โ€‹โ€‹may be a higher leak than UNION )

StuffHappens uses the UNPIVOT operator to get the same behavior as splitting the three required columns into a single result set. Personally, I would go with UNION , mainly because the syntax of UNPIVOT harder to understand.

+1
source

Normalizing a table so that there are no multiple columns is the best answer.

G Mastro's answer is good, given the existing table design.

How it would look normalized ... so you could just do:

 delete from Element where Id in ( select EleId from GroupElements where Group_id = 1 ) *Cluster* Id Group 1 1 2 1 3 2 *GroupElements* Group_id EleId 1 1 1 2 1 3 1 4 1 9 2 5 2 8 2 7 *Element* Id Name 1 'A' 2 'b' 3 'c' 4 'd' 5 'z' 6 'j' 7 't' 8 'r' 9 'e' 
+1
source
 DELETE FROM Element WHERE Id IN (SELECT Ele1 FROM Cluster WHERE Cluster.Group= 1) AND Id IN (SELECT Ele2 FROM Cluster WHERE Cluster.Group= 1) AND Id IN (SELECT Ele3 FROM Cluster WHERE Cluster.Group= 1) 
0
source

You do not select an identifier column from a cluster in a subquery. Try:

 DELETE FROM Element WHERE Id IN (SELECT Id FROM Cluster WHERE Cluster.Group= 1) 
-1
source

Source: https://habr.com/ru/post/1482563/


All Articles