select row having one kind of value only while missing other from a pair
I have to select row which has one kind of value in one row but not the other row, both row having a key
entity common
Sample model :
+------+---------+---------------+
| key | status | arrival_p |
+------+---------+---------------+
| k1 | failure | came |
| k1 | success | gone |
| k2 | failure | came |
| k3 | success | came |
| k3 | failure | gone |
| k4 | success | came |
| k5 | success | came |
| k2 | success | gone |
| k6 | success | gone |
+------+---------+---------------+
so in this case, except for k4
and k5
, all have come and gone. how can i find folks who have come but not gone?? k6
has just gone, so its an outlier, good to catch it but not immediate priority.
i tried below query but it doesn't work (i know of exact value in actual table which matches my description but below query returns no value at all) :
select ap1.`key`
from `arrival_pattern` ap1
left join `arrival_pattern` ap2
on ap1.`key` = ap2.`key`
where ap2.`key` is NULL
and ap1.`arrival_p` = 'came'
and ap2.`arrival_p` = 'gone'
limit 10;
any help or pointers in right direction as to what might be wrong in my join is helpful. i am on mysql.
TIA :)
mysql sql
add a comment |
I have to select row which has one kind of value in one row but not the other row, both row having a key
entity common
Sample model :
+------+---------+---------------+
| key | status | arrival_p |
+------+---------+---------------+
| k1 | failure | came |
| k1 | success | gone |
| k2 | failure | came |
| k3 | success | came |
| k3 | failure | gone |
| k4 | success | came |
| k5 | success | came |
| k2 | success | gone |
| k6 | success | gone |
+------+---------+---------------+
so in this case, except for k4
and k5
, all have come and gone. how can i find folks who have come but not gone?? k6
has just gone, so its an outlier, good to catch it but not immediate priority.
i tried below query but it doesn't work (i know of exact value in actual table which matches my description but below query returns no value at all) :
select ap1.`key`
from `arrival_pattern` ap1
left join `arrival_pattern` ap2
on ap1.`key` = ap2.`key`
where ap2.`key` is NULL
and ap1.`arrival_p` = 'came'
and ap2.`arrival_p` = 'gone'
limit 10;
any help or pointers in right direction as to what might be wrong in my join is helpful. i am on mysql.
TIA :)
mysql sql
Possible values forarrival_p
are exclusivelycame
andgone
? For each key there's at most 1 record with arrival_p=came and at most 1 record with arrival_p=gone?
– Robert Kock
Nov 21 at 8:54
yes sir...its anenum
from code with only possible values ofcame / gone
,key + arrival_p
have unique constraint
– NoobEditor
Nov 21 at 8:56
add a comment |
I have to select row which has one kind of value in one row but not the other row, both row having a key
entity common
Sample model :
+------+---------+---------------+
| key | status | arrival_p |
+------+---------+---------------+
| k1 | failure | came |
| k1 | success | gone |
| k2 | failure | came |
| k3 | success | came |
| k3 | failure | gone |
| k4 | success | came |
| k5 | success | came |
| k2 | success | gone |
| k6 | success | gone |
+------+---------+---------------+
so in this case, except for k4
and k5
, all have come and gone. how can i find folks who have come but not gone?? k6
has just gone, so its an outlier, good to catch it but not immediate priority.
i tried below query but it doesn't work (i know of exact value in actual table which matches my description but below query returns no value at all) :
select ap1.`key`
from `arrival_pattern` ap1
left join `arrival_pattern` ap2
on ap1.`key` = ap2.`key`
where ap2.`key` is NULL
and ap1.`arrival_p` = 'came'
and ap2.`arrival_p` = 'gone'
limit 10;
any help or pointers in right direction as to what might be wrong in my join is helpful. i am on mysql.
TIA :)
mysql sql
I have to select row which has one kind of value in one row but not the other row, both row having a key
entity common
Sample model :
+------+---------+---------------+
| key | status | arrival_p |
+------+---------+---------------+
| k1 | failure | came |
| k1 | success | gone |
| k2 | failure | came |
| k3 | success | came |
| k3 | failure | gone |
| k4 | success | came |
| k5 | success | came |
| k2 | success | gone |
| k6 | success | gone |
+------+---------+---------------+
so in this case, except for k4
and k5
, all have come and gone. how can i find folks who have come but not gone?? k6
has just gone, so its an outlier, good to catch it but not immediate priority.
i tried below query but it doesn't work (i know of exact value in actual table which matches my description but below query returns no value at all) :
select ap1.`key`
from `arrival_pattern` ap1
left join `arrival_pattern` ap2
on ap1.`key` = ap2.`key`
where ap2.`key` is NULL
and ap1.`arrival_p` = 'came'
and ap2.`arrival_p` = 'gone'
limit 10;
any help or pointers in right direction as to what might be wrong in my join is helpful. i am on mysql.
TIA :)
mysql sql
mysql sql
asked Nov 21 at 8:42
NoobEditor
11.2k84879
11.2k84879
Possible values forarrival_p
are exclusivelycame
andgone
? For each key there's at most 1 record with arrival_p=came and at most 1 record with arrival_p=gone?
– Robert Kock
Nov 21 at 8:54
yes sir...its anenum
from code with only possible values ofcame / gone
,key + arrival_p
have unique constraint
– NoobEditor
Nov 21 at 8:56
add a comment |
Possible values forarrival_p
are exclusivelycame
andgone
? For each key there's at most 1 record with arrival_p=came and at most 1 record with arrival_p=gone?
– Robert Kock
Nov 21 at 8:54
yes sir...its anenum
from code with only possible values ofcame / gone
,key + arrival_p
have unique constraint
– NoobEditor
Nov 21 at 8:56
Possible values for
arrival_p
are exclusively came
and gone
? For each key there's at most 1 record with arrival_p=came and at most 1 record with arrival_p=gone?– Robert Kock
Nov 21 at 8:54
Possible values for
arrival_p
are exclusively came
and gone
? For each key there's at most 1 record with arrival_p=came and at most 1 record with arrival_p=gone?– Robert Kock
Nov 21 at 8:54
yes sir...its an
enum
from code with only possible values of came / gone
, key + arrival_p
have unique constraint– NoobEditor
Nov 21 at 8:56
yes sir...its an
enum
from code with only possible values of came / gone
, key + arrival_p
have unique constraint– NoobEditor
Nov 21 at 8:56
add a comment |
2 Answers
2
active
oldest
votes
Since both came
and gone
can appear only once for a specific key, you might as well select the elements for which a single record exists:
SELECT `key`,
COUNT(*)
FROM arrival_pattern
GROUP BY `key`
HAVING COUNT(*) = 1;
This solves also the second question ('k6 has just gone').
Also, note that key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
1
@NoobEditor Also, note thatkey
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
– Madhur Bhaiya
Nov 21 at 9:04
1
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
add a comment |
use not exists
select t1.* from arrival_pattern t1
where not exists ( select 1
from arrival_pattern t2
where t2.key=t1.key
and t2.arrival_p='gone')
you can try below self join
select t1.* arrival_pattern t1
left join
(select key from
arrival_pattern t2 where arrival_p='gone'
) t2
on t1.key=t2.key where t2.key is null
any way usingjoins
??
– NoobEditor
Nov 21 at 8:45
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
what exactly isselect 1
doing innot exist
this case????
– NoobEditor
Nov 21 at 8:48
1
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
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%2f53408142%2fselect-row-having-one-kind-of-value-only-while-missing-other-from-a-pair%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since both came
and gone
can appear only once for a specific key, you might as well select the elements for which a single record exists:
SELECT `key`,
COUNT(*)
FROM arrival_pattern
GROUP BY `key`
HAVING COUNT(*) = 1;
This solves also the second question ('k6 has just gone').
Also, note that key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
1
@NoobEditor Also, note thatkey
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
– Madhur Bhaiya
Nov 21 at 9:04
1
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
add a comment |
Since both came
and gone
can appear only once for a specific key, you might as well select the elements for which a single record exists:
SELECT `key`,
COUNT(*)
FROM arrival_pattern
GROUP BY `key`
HAVING COUNT(*) = 1;
This solves also the second question ('k6 has just gone').
Also, note that key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
1
@NoobEditor Also, note thatkey
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
– Madhur Bhaiya
Nov 21 at 9:04
1
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
add a comment |
Since both came
and gone
can appear only once for a specific key, you might as well select the elements for which a single record exists:
SELECT `key`,
COUNT(*)
FROM arrival_pattern
GROUP BY `key`
HAVING COUNT(*) = 1;
This solves also the second question ('k6 has just gone').
Also, note that key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
Since both came
and gone
can appear only once for a specific key, you might as well select the elements for which a single record exists:
SELECT `key`,
COUNT(*)
FROM arrival_pattern
GROUP BY `key`
HAVING COUNT(*) = 1;
This solves also the second question ('k6 has just gone').
Also, note that key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
edited Nov 21 at 9:04
Madhur Bhaiya
19.5k62236
19.5k62236
answered Nov 21 at 9:01
Robert Kock
3,9591617
3,9591617
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
1
@NoobEditor Also, note thatkey
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
– Madhur Bhaiya
Nov 21 at 9:04
1
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
add a comment |
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
1
@NoobEditor Also, note thatkey
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.
– Madhur Bhaiya
Nov 21 at 9:04
1
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
ohh maannnn....sometimes overthinking kills the simple solution, this is clean +1!!!
– NoobEditor
Nov 21 at 9:04
1
1
@NoobEditor Also, note that
key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.– Madhur Bhaiya
Nov 21 at 9:04
@NoobEditor Also, note that
key
is a Reserved Keyword in MySQL. You should seriously consider naming your column to something else.– Madhur Bhaiya
Nov 21 at 9:04
1
1
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@MadhurBhaiya : this is just a reference schema buddy, dont go by my screen name, m not really that noob! :D
– NoobEditor
Nov 21 at 9:13
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
@NoobEditor ;) well I am not a telepath :P
– Madhur Bhaiya
Nov 21 at 9:14
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
If any key just contain 'gone' that key also return on this query but if your real data is like sample no exception that I thought then it will work fine
– Zaynul Abadin Tuhin
Nov 21 at 14:10
add a comment |
use not exists
select t1.* from arrival_pattern t1
where not exists ( select 1
from arrival_pattern t2
where t2.key=t1.key
and t2.arrival_p='gone')
you can try below self join
select t1.* arrival_pattern t1
left join
(select key from
arrival_pattern t2 where arrival_p='gone'
) t2
on t1.key=t2.key where t2.key is null
any way usingjoins
??
– NoobEditor
Nov 21 at 8:45
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
what exactly isselect 1
doing innot exist
this case????
– NoobEditor
Nov 21 at 8:48
1
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
add a comment |
use not exists
select t1.* from arrival_pattern t1
where not exists ( select 1
from arrival_pattern t2
where t2.key=t1.key
and t2.arrival_p='gone')
you can try below self join
select t1.* arrival_pattern t1
left join
(select key from
arrival_pattern t2 where arrival_p='gone'
) t2
on t1.key=t2.key where t2.key is null
any way usingjoins
??
– NoobEditor
Nov 21 at 8:45
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
what exactly isselect 1
doing innot exist
this case????
– NoobEditor
Nov 21 at 8:48
1
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
add a comment |
use not exists
select t1.* from arrival_pattern t1
where not exists ( select 1
from arrival_pattern t2
where t2.key=t1.key
and t2.arrival_p='gone')
you can try below self join
select t1.* arrival_pattern t1
left join
(select key from
arrival_pattern t2 where arrival_p='gone'
) t2
on t1.key=t2.key where t2.key is null
use not exists
select t1.* from arrival_pattern t1
where not exists ( select 1
from arrival_pattern t2
where t2.key=t1.key
and t2.arrival_p='gone')
you can try below self join
select t1.* arrival_pattern t1
left join
(select key from
arrival_pattern t2 where arrival_p='gone'
) t2
on t1.key=t2.key where t2.key is null
edited Nov 21 at 8:48
answered Nov 21 at 8:44
Zaynul Abadin Tuhin
11.3k2831
11.3k2831
any way usingjoins
??
– NoobEditor
Nov 21 at 8:45
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
what exactly isselect 1
doing innot exist
this case????
– NoobEditor
Nov 21 at 8:48
1
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
add a comment |
any way usingjoins
??
– NoobEditor
Nov 21 at 8:45
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
what exactly isselect 1
doing innot exist
this case????
– NoobEditor
Nov 21 at 8:48
1
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
any way using
joins
??– NoobEditor
Nov 21 at 8:45
any way using
joins
??– NoobEditor
Nov 21 at 8:45
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
@NoobEditor yes you can use self join
– Zaynul Abadin Tuhin
Nov 21 at 8:46
what exactly is
select 1
doing in not exist
this case????– NoobEditor
Nov 21 at 8:48
what exactly is
select 1
doing in not exist
this case????– NoobEditor
Nov 21 at 8:48
1
1
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
@NoobEditor actually EXISTS operator returns true if the subquery returns one or more records so i select constant value 1 rather any column selection you can read docs of sql how exists work
– Zaynul Abadin Tuhin
Nov 21 at 8:50
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53408142%2fselect-row-having-one-kind-of-value-only-while-missing-other-from-a-pair%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
Possible values for
arrival_p
are exclusivelycame
andgone
? For each key there's at most 1 record with arrival_p=came and at most 1 record with arrival_p=gone?– Robert Kock
Nov 21 at 8:54
yes sir...its an
enum
from code with only possible values ofcame / gone
,key + arrival_p
have unique constraint– NoobEditor
Nov 21 at 8:56