Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

T-SQL - One to many exists clause

Posted on 2015-01-22
17
Medium Priority
?
95 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 52

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 52

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
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.

 
LVL 25

Expert Comment

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

Accepted Solution

by:
Scott Pletcher earned 2000 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 70

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 52

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 70

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 52

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 70

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 52

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 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

618 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