我有这样的表:
Serial IO
----------------------------------------------------
1 i
2 i
3 i
1 o
预期结果:o
列
IO
的所有序列号
Serial IO
-----------------------------------------------------
2 i
3 i
例如,序列1
在i
列中同时包含o
和IO
,这就是它不会出现在结果窗格中的原因。
答案 0 :(得分:4)
您可以使用not exists
:
select serial, io
from t
where not exists (select 1 from t t2 where t2.serial = t.serial and t2.io = 'o');
答案 1 :(得分:2)
只是一种方式:
WITH i_table AS (SELECT serial,IO FROM my_table WHERE IO = 'i'),
o_table AS (SELECT serial,IO FROM my_table WHERE IO = 'o')
SELECT DISTINCT i.*
FROM i_table AS i
LEFT JOIN o_table AS o
ON i.serial = o.serial
WHERE o.serial IS NULL;
答案 2 :(得分:2)
NOT EXISTS
和类似的方法有效。根据使用Windows /分析函数的索引,可以有不同的性能配置文件......
WITH
sized AS
(
SELECT
yourTable.*,
COUNT(*) OVER (PARTITION BY IO) AS group_size
FROM
yourTable
)
SELECT
*
FROM
sized
WHERE
group_size = 1
答案 3 :(得分:2)
请尝试以下代码。
select t.Serial,t.IO from table1 t inner join
(select Serial from table1 group by Serial
having count(distinct IO) = 1) t2 ON t.Serial = t2.Serial
where t.IO != 'o'
答案 4 :(得分:2)
试试这个:
select distinct * from MY_TABLE
where [Serial] not in (select [Serial] from MY_TABLE where [IO] = 'o')
答案 5 :(得分:2)
您可以离开加入并排除那些匹配
的人SELECT
a.Serial,
a.[IO]
FROM <table> a
LEFT JOIN
<table> b
ON a.Serial = b.Serial
and b.[IO] = 'o'
WHERE
a.[IO] = 'i'
and b.[IO] is null
答案 6 :(得分:0)
不是最有效但很短的
select Serial, io from table where 'io' = 'i'
except
select Serial, 'i' from table where 'io' = 'o'