select row having one kind of value only while missing other from a pair
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.1k84878
11.1k84878
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
up vote
3
down vote
accepted
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 |
up vote
1
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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 |
up vote
3
down vote
accepted
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 |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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
18.5k62236
18.5k62236
answered Nov 21 at 9:01
Robert Kock
3,7541616
3,7541616
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 |
up vote
1
down vote
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 |
up vote
1
down vote
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 |
up vote
1
down vote
up vote
1
down vote
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
10.8k2731
10.8k2731
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 |
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