Solved

T-SQL - One to many exists clause

Posted on 2015-01-22
17
78 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 45

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 45

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
 
LVL 25

Expert Comment

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

Accepted Solution

by:
ScottPletcher 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:ScottPletcher
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 45

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 69

Expert Comment

by:ScottPletcher
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 45

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:ScottPletcher
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 45

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:ScottPletcher
ID: 40564552
Of course, but that doesn't meant aRecordID is necessarily a "primary key" (PK), as you stated.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 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

21 Experts available now in Live!

Get 1:1 Help Now