Solved

T-SQL - One to many exists clause

Posted on 2015-01-22
17
86 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
  • 5
  • 5
  • 4
  • +1
17 Comments
 
LVL 49

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 49

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 49

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 49

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 49

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

733 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