Solved

T-SQL - One to many exists clause

Posted on 2015-01-22
17
87 Views
Last Modified: 2015-01-22
Tbl A column aRecordID.
Tbl B will have many corresponding records with aRecordID.
(Tbl A to Tbl B is one to many)

Tbl B has a column called productID. I need to grab all of the aRecordID's from Tbl A where ALL of the related records in Tbl B have productID = 1. If any of the related records in Tbl B has anything other than productID = 1, then I don't want the aRecordID from Tbl A.

Hope that makes sense. Thanks in advance!!
0
Comment
Question by:Rickzzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
17 Comments
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40564389
SELECT TblA.aRecordID, TblB.productID 
FROM TblA
    INNER JOIN TblB ON TblA.aRecordID=TblB.aRecordID
WHERE TblB.productID = 1

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40564393
On what column do the tables relate?
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40564394
Or since it will return duplicate records (one to many relationship) you can use the keyword DISTINCT to show less records:
SELECT DISTINCT TblA.aRecordID, TblB.productID 
FROM TblA
    INNER JOIN TblB ON TblA.aRecordID=TblB.aRecordID
WHERE TblB.productID = 1

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 40564395
Ahh, ignore me, I missed that.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40564401
SELECT a.*, b.* --<<-- change column list to only the columns you want to show
FROM tableA a
INNER JOIN (
    SELECT recordID
    FROM tableB
    GROUP BY recordID
    HAVING SUM(CASE WHEN productID = 1 THEN 1 ELSE 0 END) = SUM(1)
) AS b_group ON
    b_group.recordID = a.recordID
INNER JOIN tableB b ON
    b.recordID = b_group.recordID
0
 

Author Comment

by:Rickzzz
ID: 40564415
Thanks!
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40564467
That code does NOT meet your original conditions:

I need to grab all of the aRecordID's from Tbl A where ALL of the related records in Tbl B have productID = 1. If any of the related records in Tbl B has anything other than productID = 1, then I don't want the aRecordID from Tbl A.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40564477
I think the author wanted to say that he only want records with ProductID=1 and not interested in others products. At least is what I understood.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40564495
It's very clear that was NOT the original requirement:

If any of the related records in Tbl B has anything other than productID = 1, then I don't want the aRecordID from Tbl A.

That's perfectly clear, absolutely unambiguous.
0
 

Author Comment

by:Rickzzz
ID: 40564502
Actually I just realized this should have gone to Scott Pletcher.
0
 

Author Comment

by:Rickzzz
ID: 40564505
Yes, I selected the wrong one. Is there an admin that can fix.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40564513
Strange for not wanting the aRecordID from TblA (PK) since is the same value of aRecordID from TblB (FK).
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40564531
[Also no indication that "aRecordID" is a primary key; I won't make that assumption either.  It could be, but that's not really known -- or relevant.]
0
 

Author Comment

by:Rickzzz
ID: 40564532
Thanks, yes don't want anything from Tbl A unless the condition above exists.
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 40564541
(Tbl A to Tbl B is one to many)
(...)
I need to grab all of the aRecordID's from Tbl A where ALL of the related records in Tbl B have productID = 1. If any of the related records in Tbl B has (...)
For me this means that exists a relation between those tables by the column aRecordID.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40564552
Of course, but that doesn't meant aRecordID is necessarily a "primary key" (PK), as you stated.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question