Match ALL (not ANY) values from a WHERE clause [duplicate]
This question already has an answer here:
SQL efficient way to join a table where all values exist
3 answers
TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?
This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN
, INTERSECT
, EXISTS
or ALL
, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.
Take the following people who like different colors:
DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');
I need to find people who like ALL of the colors I'm searching for.
DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');
So I would only expect to see Bob
and Fred
in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob
likes 3 colors, including the two I need).
Reading through books online, I found ALL
, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):
SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));
tsql sql-server-2012
marked as duplicate by Zohar Peled, Community♦ Nov 14 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
SQL efficient way to join a table where all values exist
3 answers
TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?
This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN
, INTERSECT
, EXISTS
or ALL
, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.
Take the following people who like different colors:
DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');
I need to find people who like ALL of the colors I'm searching for.
DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');
So I would only expect to see Bob
and Fred
in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob
likes 3 colors, including the two I need).
Reading through books online, I found ALL
, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):
SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));
tsql sql-server-2012
marked as duplicate by Zohar Peled, Community♦ Nov 14 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.
– EvilDr
Nov 14 '18 at 8:27
With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)
– Zohar Peled
Nov 14 '18 at 8:28
add a comment |
This question already has an answer here:
SQL efficient way to join a table where all values exist
3 answers
TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?
This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN
, INTERSECT
, EXISTS
or ALL
, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.
Take the following people who like different colors:
DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');
I need to find people who like ALL of the colors I'm searching for.
DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');
So I would only expect to see Bob
and Fred
in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob
likes 3 colors, including the two I need).
Reading through books online, I found ALL
, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):
SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));
tsql sql-server-2012
This question already has an answer here:
SQL efficient way to join a table where all values exist
3 answers
TL;DR How do I find rows in a table that match ALL (not ANY) rows from another table?
This seems so simple but I don't know the correct terminology, so am seeing dozens of answers that use INNER JOIN
, INTERSECT
, EXISTS
or ALL
, but don't achieve what I need. The other questions are either PostgreSQL, dynamically generated SQL via the application, or are unanswered.
Take the following people who like different colors:
DECLARE @tbl TABLE (
FirstName nvarchar(50),
Color nvarchar(50)
);
INSERT INTO @tbl
(FirstName, Color)
VALUES
('Bob', 'Purple'),
('Bob', 'Red'),
('Bob', 'Yellow'),
('Fred', 'Purple'),
('Fred', 'Red'),
('Fred', 'Yellow'),
('Greg', 'Orange'),
('Greg', 'Red'),
('Harry', 'Red');
I need to find people who like ALL of the colors I'm searching for.
DECLARE @SearchColors TABLE (SearchColor nvarchar(50));
INSERT INTO @SearchColors (SearchColor) VALUES ('Red'),('Yellow');
So I would only expect to see Bob
and Fred
in the results, because only those two people like ALL of the colours I'm searching for. I don't want people who only like a single colour, however, it doesn't matter if people like more than both of those colours (e.g. Bob
likes 3 colors, including the two I need).
Reading through books online, I found ALL
, which appeared close to what I need, but actually finds nothing (unless I'm using it wrong):
SELECT
*
FROM
@tbl
WHERE
(Color = ALL ( SELECT SearchColor FROM @SearchColors ));
This question already has an answer here:
SQL efficient way to join a table where all values exist
3 answers
tsql sql-server-2012
tsql sql-server-2012
edited Nov 13 '18 at 22:27
vencaslac
1,070317
1,070317
asked Nov 13 '18 at 22:25
EvilDrEvilDr
3,85074490
3,85074490
marked as duplicate by Zohar Peled, Community♦ Nov 14 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Zohar Peled, Community♦ Nov 14 '18 at 8:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.
– EvilDr
Nov 14 '18 at 8:27
With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)
– Zohar Peled
Nov 14 '18 at 8:28
add a comment |
Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.
– EvilDr
Nov 14 '18 at 8:27
With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)
– Zohar Peled
Nov 14 '18 at 8:28
Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.
– EvilDr
Nov 14 '18 at 8:27
Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.
– EvilDr
Nov 14 '18 at 8:27
With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)
– Zohar Peled
Nov 14 '18 at 8:28
With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)
– Zohar Peled
Nov 14 '18 at 8:28
add a comment |
1 Answer
1
active
oldest
votes
What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?
DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)
SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount
This will handle dupes in your @tbl
if ('Greg', 'Red')
is in there twice or if your @searchcolors
table has ('Red')
twice
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?
DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)
SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount
This will handle dupes in your @tbl
if ('Greg', 'Red')
is in there twice or if your @searchcolors
table has ('Red')
twice
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
add a comment |
What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?
DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)
SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount
This will handle dupes in your @tbl
if ('Greg', 'Red')
is in there twice or if your @searchcolors
table has ('Red')
twice
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
add a comment |
What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?
DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)
SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount
This will handle dupes in your @tbl
if ('Greg', 'Red')
is in there twice or if your @searchcolors
table has ('Red')
twice
What about getting the total count of colors you are looking for, filtering your main table for those, then counting names that have that many entries?
DECLARE @colorcount INT = (SELECT COUNT(DISTINCT SearchColor) FROM @SearchColors)
SELECT firstname
FROM @tbl
WHERE color IN (SELECT searchcolor FROM @SearchColors)
GROUP BY firstname
HAVING COUNT(DISTINCT color) = @colorcount
This will handle dupes in your @tbl
if ('Greg', 'Red')
is in there twice or if your @searchcolors
table has ('Red')
twice
edited Nov 13 '18 at 22:36
answered Nov 13 '18 at 22:30
dfundakodfundako
5,0433923
5,0433923
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
add a comment |
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
Thank you. I was massively overthinking this with an overtired mind...
– EvilDr
Nov 14 '18 at 8:28
add a comment |
Yes I believe your answer in that post does answer the question, although I never saw that question listed when I was searching (late) last night.
– EvilDr
Nov 14 '18 at 8:27
With so many questions on stackoverflow, don't feel bad for posting a duplicate. It's very hard to post a question that is not a duplicate :-)
– Zohar Peled
Nov 14 '18 at 8:28