SQL Select 一个值,但也要考虑其他值
SQL Select one value but take in account other values as well
这就是 table 的样子
Number UniqID status
555 1 in
555 1 in
555 1 in
555 2 in
555 2 out
555 2 in
我想select这样
Number UniqID status
555 1 in
555 1 in
555 1 in
并且只有 select 如果 所有 相同的 uniqID 都有状态。如果其中一个状态表明相同的 ID,则跳过整个过程。 UniqID 也是自动生成的
并且想展示它
Number status
555 in
您可以通过 not exists
获得第一个结果集:
select *
from mytable t
where
t.status = 'in'
and not exists (
select 1
from mytable t1
where t1.number = t.number and t1.uniqid = t.uniqid and t1.status = 'out'
)
另一方面,如果您想要所有 (number, uniqid)
个所有状态都为“in”的元组,则聚合更简单:
select number, uniqid, min(status) as status
from mytable
group by number, uniqid
having min(status) = max(status) and min(status) = 'in'
这就是 table 的样子
Number UniqID status
555 1 in
555 1 in
555 1 in
555 2 in
555 2 out
555 2 in
我想select这样
Number UniqID status
555 1 in
555 1 in
555 1 in
并且只有 select 如果 所有 相同的 uniqID 都有状态。如果其中一个状态表明相同的 ID,则跳过整个过程。 UniqID 也是自动生成的
并且想展示它
Number status
555 in
您可以通过 not exists
获得第一个结果集:
select *
from mytable t
where
t.status = 'in'
and not exists (
select 1
from mytable t1
where t1.number = t.number and t1.uniqid = t.uniqid and t1.status = 'out'
)
另一方面,如果您想要所有 (number, uniqid)
个所有状态都为“in”的元组,则聚合更简单:
select number, uniqid, min(status) as status
from mytable
group by number, uniqid
having min(status) = max(status) and min(status) = 'in'