SQL filtering query based on number of related rows
I have trouble wrapping my head around this problem.
After processing some queries I end up with a result of this kind:
SELECT col1, col2 FROM Whatever;
col1 col2
1 100
2 200
3 200
2 100
And I want to pull out 2
out of this, as 2
is the only value of col1
such that it corresponds to every existing value of col2
(100
and 200
). If there was another row, say, 4 400
then I would want an empty result, as there would be no rows fulfilling this condition.
I was thinking ALL
could help me, but then I realised that it's useless. I'd appreciate even athe slightest hint as to what to read.
Thanks in advance.
UPD: After some digging I came to conclusion that I need relational division, i.e.
Whatever <relational division> SELECT DISTINCT col2 FROM Whatever
sql
add a comment |
I have trouble wrapping my head around this problem.
After processing some queries I end up with a result of this kind:
SELECT col1, col2 FROM Whatever;
col1 col2
1 100
2 200
3 200
2 100
And I want to pull out 2
out of this, as 2
is the only value of col1
such that it corresponds to every existing value of col2
(100
and 200
). If there was another row, say, 4 400
then I would want an empty result, as there would be no rows fulfilling this condition.
I was thinking ALL
could help me, but then I realised that it's useless. I'd appreciate even athe slightest hint as to what to read.
Thanks in advance.
UPD: After some digging I came to conclusion that I need relational division, i.e.
Whatever <relational division> SELECT DISTINCT col2 FROM Whatever
sql
Tag your question with the database you are using.
– Gordon Linoff
Nov 21 '18 at 16:23
@GordonLinoff I'm interested specifically in vanilla SQL, not the implementations of it. I don't want use any implementation-specific stuff.
– Всеволод Тимченко
Nov 21 '18 at 16:32
@YogeshSharma WhyHAVING COUNT(*) > 1
though? There's nothing in my question that implies anything related to count larger than 1.
– Всеволод Тимченко
Nov 21 '18 at 16:33
add a comment |
I have trouble wrapping my head around this problem.
After processing some queries I end up with a result of this kind:
SELECT col1, col2 FROM Whatever;
col1 col2
1 100
2 200
3 200
2 100
And I want to pull out 2
out of this, as 2
is the only value of col1
such that it corresponds to every existing value of col2
(100
and 200
). If there was another row, say, 4 400
then I would want an empty result, as there would be no rows fulfilling this condition.
I was thinking ALL
could help me, but then I realised that it's useless. I'd appreciate even athe slightest hint as to what to read.
Thanks in advance.
UPD: After some digging I came to conclusion that I need relational division, i.e.
Whatever <relational division> SELECT DISTINCT col2 FROM Whatever
sql
I have trouble wrapping my head around this problem.
After processing some queries I end up with a result of this kind:
SELECT col1, col2 FROM Whatever;
col1 col2
1 100
2 200
3 200
2 100
And I want to pull out 2
out of this, as 2
is the only value of col1
such that it corresponds to every existing value of col2
(100
and 200
). If there was another row, say, 4 400
then I would want an empty result, as there would be no rows fulfilling this condition.
I was thinking ALL
could help me, but then I realised that it's useless. I'd appreciate even athe slightest hint as to what to read.
Thanks in advance.
UPD: After some digging I came to conclusion that I need relational division, i.e.
Whatever <relational division> SELECT DISTINCT col2 FROM Whatever
sql
sql
edited Nov 21 '18 at 18:53
Всеволод Тимченко
asked Nov 21 '18 at 16:21
Всеволод ТимченкоВсеволод Тимченко
10211
10211
Tag your question with the database you are using.
– Gordon Linoff
Nov 21 '18 at 16:23
@GordonLinoff I'm interested specifically in vanilla SQL, not the implementations of it. I don't want use any implementation-specific stuff.
– Всеволод Тимченко
Nov 21 '18 at 16:32
@YogeshSharma WhyHAVING COUNT(*) > 1
though? There's nothing in my question that implies anything related to count larger than 1.
– Всеволод Тимченко
Nov 21 '18 at 16:33
add a comment |
Tag your question with the database you are using.
– Gordon Linoff
Nov 21 '18 at 16:23
@GordonLinoff I'm interested specifically in vanilla SQL, not the implementations of it. I don't want use any implementation-specific stuff.
– Всеволод Тимченко
Nov 21 '18 at 16:32
@YogeshSharma WhyHAVING COUNT(*) > 1
though? There's nothing in my question that implies anything related to count larger than 1.
– Всеволод Тимченко
Nov 21 '18 at 16:33
Tag your question with the database you are using.
– Gordon Linoff
Nov 21 '18 at 16:23
Tag your question with the database you are using.
– Gordon Linoff
Nov 21 '18 at 16:23
@GordonLinoff I'm interested specifically in vanilla SQL, not the implementations of it. I don't want use any implementation-specific stuff.
– Всеволод Тимченко
Nov 21 '18 at 16:32
@GordonLinoff I'm interested specifically in vanilla SQL, not the implementations of it. I don't want use any implementation-specific stuff.
– Всеволод Тимченко
Nov 21 '18 at 16:32
@YogeshSharma Why
HAVING COUNT(*) > 1
though? There's nothing in my question that implies anything related to count larger than 1.– Всеволод Тимченко
Nov 21 '18 at 16:33
@YogeshSharma Why
HAVING COUNT(*) > 1
though? There's nothing in my question that implies anything related to count larger than 1.– Всеволод Тимченко
Nov 21 '18 at 16:33
add a comment |
1 Answer
1
active
oldest
votes
One method uses window functions:
select col1, col2
from (select col1, col2,
count(distinct col2) over () as num_col2,
count(distinct col2) over (partition by col1) as num_col2_per_col1
from whatever
) t
where num_col2 = num_col2_per_col1;
Not all databases support count(distinct)
as a window function. There are pretty simply work-arounds, if that is necessary, but count(distinct)
as a window function is standard SQL.
If you only want "2" and not all the rows, then use select distinct col1
.
1
.great answer!!
– stack0114106
Jan 16 at 2:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416381%2fsql-filtering-query-based-on-number-of-related-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One method uses window functions:
select col1, col2
from (select col1, col2,
count(distinct col2) over () as num_col2,
count(distinct col2) over (partition by col1) as num_col2_per_col1
from whatever
) t
where num_col2 = num_col2_per_col1;
Not all databases support count(distinct)
as a window function. There are pretty simply work-arounds, if that is necessary, but count(distinct)
as a window function is standard SQL.
If you only want "2" and not all the rows, then use select distinct col1
.
1
.great answer!!
– stack0114106
Jan 16 at 2:56
add a comment |
One method uses window functions:
select col1, col2
from (select col1, col2,
count(distinct col2) over () as num_col2,
count(distinct col2) over (partition by col1) as num_col2_per_col1
from whatever
) t
where num_col2 = num_col2_per_col1;
Not all databases support count(distinct)
as a window function. There are pretty simply work-arounds, if that is necessary, but count(distinct)
as a window function is standard SQL.
If you only want "2" and not all the rows, then use select distinct col1
.
1
.great answer!!
– stack0114106
Jan 16 at 2:56
add a comment |
One method uses window functions:
select col1, col2
from (select col1, col2,
count(distinct col2) over () as num_col2,
count(distinct col2) over (partition by col1) as num_col2_per_col1
from whatever
) t
where num_col2 = num_col2_per_col1;
Not all databases support count(distinct)
as a window function. There are pretty simply work-arounds, if that is necessary, but count(distinct)
as a window function is standard SQL.
If you only want "2" and not all the rows, then use select distinct col1
.
One method uses window functions:
select col1, col2
from (select col1, col2,
count(distinct col2) over () as num_col2,
count(distinct col2) over (partition by col1) as num_col2_per_col1
from whatever
) t
where num_col2 = num_col2_per_col1;
Not all databases support count(distinct)
as a window function. There are pretty simply work-arounds, if that is necessary, but count(distinct)
as a window function is standard SQL.
If you only want "2" and not all the rows, then use select distinct col1
.
edited Nov 21 '18 at 16:31
answered Nov 21 '18 at 16:25
Gordon LinoffGordon Linoff
789k35313418
789k35313418
1
.great answer!!
– stack0114106
Jan 16 at 2:56
add a comment |
1
.great answer!!
– stack0114106
Jan 16 at 2:56
1
1
.great answer!!
– stack0114106
Jan 16 at 2:56
.great answer!!
– stack0114106
Jan 16 at 2:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416381%2fsql-filtering-query-based-on-number-of-related-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Tag your question with the database you are using.
– Gordon Linoff
Nov 21 '18 at 16:23
@GordonLinoff I'm interested specifically in vanilla SQL, not the implementations of it. I don't want use any implementation-specific stuff.
– Всеволод Тимченко
Nov 21 '18 at 16:32
@YogeshSharma Why
HAVING COUNT(*) > 1
though? There's nothing in my question that implies anything related to count larger than 1.– Всеволод Тимченко
Nov 21 '18 at 16:33