Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Delete Row from one table if other table with multiple rows meet the condition Expand / Collapse
Author
Message
Posted Monday, October 7, 2013 1:15 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 AM
Points: 163, Visits: 574
In TableA We don't have Product Column, It only in TableB
Post #1502324
Posted Monday, October 7, 2013 1:19 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:12 PM
Points: 13,441, Visits: 12,303
RamSteve (10/7/2013)
In TableA We don't have Product Column, It only in TableB


You really need to quote the original post in your responses so we have a clue what you are referring to.

We are all shooting in the dark here because we have no knowledge of your system and you didn't provide ddl for us. Please take a few minutes and read the first article in my signature.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1502327
Posted Monday, October 7, 2013 1:19 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 4:52 PM
Points: 590, Visits: 911
You need to provide some DDL for this problem. How about some create table statements with actual column names and real data with expected results.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1502328
Posted Monday, October 7, 2013 11:43 PM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Wednesday, December 18, 2013 5:03 AM
Points: 1,454, Visits: 135
Hi,

Try with this code


delete from tableA where ConsumerID in(
select ConsumerID from tableB where ConsumerID not in(
select ConsumerID from tableB
where DeleteFlag = 0))

Post #1502465
Posted Wednesday, October 9, 2013 3:10 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 3:25 AM
Points: 718, Visits: 547
CREATE TABLE [A]
(consumerID INT, XXXX INT, AAAAA INT, BBBB INT)

CREATE TABLE [B]
(consumerID INT, Product INT, DeleteFlag BIT, DDDD INT)

INSERT INTO A VALUES (99,1,2,3)
INSERT INTO A VALUES (100,2,3,4)

INSERT INTO B VALUES (99,1,1,4)
INSERT INTO B VALUES (99,2,1,5)
INSERT INTO B VALUES (99,1,0,6)
INSERT INTO B VALUES (99,5,1,3)
Post #1502978
Posted Wednesday, October 9, 2013 3:11 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 3:25 AM
Points: 718, Visits: 547

DELETE A FROM A INNER JOIN B ON A.consumerID = B.consumerID WHERE A.consumerID = 99 AND B.deleteflag = 1
Post #1502979
Posted Wednesday, October 9, 2013 4:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 6:15 AM
Points: 3, Visits: 172
Using Co-related query:

DELETE FROM tablea
WHERE ConsumerID IN
(SELECT ConsumerID FROM Tableb b WHERE tablea.ConsumerID=b.ConsumerID and b.DeleteFlag=1)
AND ConsumerID=99
Post #1503004
Posted Wednesday, October 9, 2013 5:57 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 2:35 PM
Points: 745, Visits: 1,199
Using a common_table_expression can make it more readable;

;WITH ToDelete (ConsumerId) AS (
SELECT ConsumerId FROM Table_B WHERE DeleteFlag = 1
EXCEPT
SELECT ConsumerId FROM Table_B WHERE DeleteFlag = 0
)
DELETE TABLE_A
FROM TABLE_A INNER JOIN
ToDelete ON TABLE_A.ComsumerId = ToDelete.ConsumerId

Post #1503040
Posted Wednesday, October 9, 2013 9:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 6:15 AM
Points: 3, Visits: 172
I believe Louis Hillebrand's query deletes all record. Need to add ConsumerID=99 in condition.
Post #1503178
Posted Wednesday, October 9, 2013 9:47 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 8:35 AM
Points: 163, Visits: 574
here applying condition 99 is not needed as i need to delete all the rows in TableA with the same condition WHERE deleteflag is 1 for all the products
Post #1503179
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse