SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete Row from one table if other table with multiple rows meet the condition


Delete Row from one table if other table with multiple rows meet the condition

Author
Message
RamSteve
RamSteve
SSC Eights!
SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)

Group: General Forum Members
Points: 826 Visits: 640
Hi I have 2 Tables as below , I need to delete TableA row with ConsumerID = 99 as my condition would be to delete the rows in Table A if DeleteFlag in Table B is 1 for the columns ConsumerID and Product
Can some one help me deleting the Table A With Consumer ID = 99 with sql script


thanks in Advance Smile


Table A :
ConsumerID, xxxx, AAAAA, BBBB
99 1 2 3
100 2 3 4

Table B :
ConsumerID, Product, DeleteFlag, dddd

99 1 1 4
99 2 1 5
100 1 0 6
100 5 1 3
Keith Tate
Keith Tate
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2398 Visits: 979
This looks like a homework assignment or something. You need to look up how to do a join in a delete statement or using the EXISTS statement.



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
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65014 Visits: 17979
RamSteve (10/7/2013)
Hi I have 2 Tables as below , I need to delete TableA row with ConsumerID = 99 as my condition would be to delete the rows in Table A if DeleteFlag in Table B is 1 for the columns ConsumerID and Product
Can some one help me deleting the Table A With Consumer ID = 99 with sql script


thanks in Advance Smile


Table A :
ConsumerID, xxxx, AAAAA, BBBB
99 1 2 3
100 2 3 4

Table B :
ConsumerID, Product, DeleteFlag, dddd

99 1 1 4
99 2 1 5
100 1 0 6
100 5 1 3


There are obviously some additional business rules missing from your description. Why would you not also delete ConsumerID 100? There is a row with DeleteFlag of 1 in TableB.

As previously stated, this looks a lot like homework. We will help you figure out a solution but we don't do homework for you. If you we do the work, you don't learn anything.

_______________________________________________________________

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 Modens 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)
RamSteve
RamSteve
SSC Eights!
SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)

Group: General Forum Members
Points: 826 Visits: 640
Hi i do not want to delete consumerID 100 as deleteflag is 0 for one product
and moreover this is not a home work , but i am trying to do it in a simple way for the below query

DELETE Table A
FROM TableA a
INNER JOIN TableB b ON b.ConsumerID = a.ConsumerID
WHERE a.ConsumerID IN
(
SELECT ConsumerID FROM TableB WHERE DeleteFlag= 1 AND ConsumerID NOT IN (SELECT Consumer_ID FROM Visitor_By_Brand WHERE Delete_Flag =0 )
)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65014 Visits: 17979
RamSteve (10/7/2013)
Hi i do not want to delete consumerID 100 as deleteflag is 0 for one product
and moreover this is not a home work , but i am trying to do it in a simple way for the below query

DELETE Table A
FROM TableA a
INNER JOIN TableB b ON b.ConsumerID = a.ConsumerID
WHERE a.ConsumerID IN
(
SELECT ConsumerID FROM TableB WHERE DeleteFlag= 1 AND ConsumerID NOT IN (SELECT Consumer_ID FROM Visitor_By_Brand WHERE Delete_Flag =0 )
)



Your query as posted here would still delete 100 assuming there is not a record in Visitor_By_Brand where Delete_Flag = 0 for that consumer.

_______________________________________________________________

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 Modens 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)
RamSteve
RamSteve
SSC Eights!
SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)

Group: General Forum Members
Points: 826 Visits: 640
my query would be as below

DELETE Table A
FROM TableA a
INNER JOIN TableB b ON b.ConsumerID = a.ConsumerID
WHERE a.ConsumerID IN
(
SELECT ConsumerID FROM TableB WHERE DeleteFlag= 1 AND ConsumerID NOT IN (SELECT ConsumerID FROM TableB WHERE DeleteFlag =0 )
)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65014 Visits: 17979
I think you could use this for the same. It is a bit simpler but should accomplish the same thing.


DELETE TableA
FROM TableA a
WHERE a.ConsumerID IN
(
SELECT ConsumerID
FROM TableB
group by ConsumerID
HAVING MIN(DeleteFlag) = 1
)



_______________________________________________________________

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 Modens 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)
RamSteve
RamSteve
SSC Eights!
SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)

Group: General Forum Members
Points: 826 Visits: 640
I guess this Query will delete all rows in TableA
LinksUp
LinksUp
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1771 Visits: 4640
There is a little bit of guessing here.

I need to delete TableA row with ConsumerID = 99 as my condition would be to delete the rows in Table A if DeleteFlag in Table B is 1 for the columns ConsumerID and Product


Based on your original request, would this be the right interpretation?


if A.ID = B.ID and A.xxxx = B.Product_ID and B.DeleteFlag = 1 then
Delete from TableA
end



If so, then this could work:


delete a
from TableA a
inner join TableB b on b.ID = a. ID and b.PID = a.xxxx --guessing this is a product id
where b.DeleteFlag = 1



__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65014 Visits: 17979
RamSteve (10/7/2013)
I guess this Query will delete all rows in TableA


Which query?

_______________________________________________________________

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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search