Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - help deleting rows with a multiple where clause using a sub query

Tags:

sql

sql-server

I am having some trouble with a SQL delete. SQL Server doesn't like have multiple parameters in the where clause to delete rows from table_02 (subquery is table 1). Any help on this would greatly be appreciated.

thanks.

DELETE FROM table_02
        WHERE (col_1,col_2,col_3,col_4)
        IN (            
            SELECT col_1,col_2,col_3,col_4 
                    FROM table_01
                    GROUP BY
                    col_1,col_2,col_3,col_4
                    HAVING SUM(CASE WHEN col_1<6 THEN col_2*-1 ELSE col_2 END)=0
           )
like image 305
tray Avatar asked Aug 19 '26 21:08

tray


1 Answers

You can rewrite IN as an EXISTS

DELETE 
FROM table_02
WHERE  EXISTS(SELECT *
              FROM   table_01
              WHERE  table_02.col_1 = table_01.col_1
                     AND table_02.col_2 = table_01.col_2
                     AND table_02.col_3 = table_01.col_3
                     AND table_02.col_4 = table_01.col_4
              HAVING SUM(CASE
                           WHEN col_1 < 6 THEN col_2 * -1
                           ELSE col_2
                         END) = 0)  
like image 199
Martin Smith Avatar answered Aug 21 '26 16:08

Martin Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!