Solved

T-SQL - One to many exists clause

Posted on 2015-01-22
17
81 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 47

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 47

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 47

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 47

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 47

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now