SQL - 选择具有不匹配的空行的所有行
SQL - Selecting all rows with non matching null rows
如何 select 所有具有不匹配空行的行?给定以下 table,不应返回任何具有外键 1 的行,因为存在具有 NULL 的对应行。我怎么可能只有 select 行带有外键 2 和 3?
foreign_key | created_at
1 12345...
1 12345...
2 12345...
3 12345...
1 NULL
您可以使用 not exists
:
select *
from mytable t
where not exists (
select 1
from mytable t1
where t1.foreign_key = t.foreign_key and t1.created_at is null
)
另一种选择是使用window函数;这是一种使用布尔值 windowing:
的方法
select *
from (
select t.*, bool_or(created_at is null) over(partition by foreignkey) has_null
from mytable t
) t
where not has_null
如何 select 所有具有不匹配空行的行?给定以下 table,不应返回任何具有外键 1 的行,因为存在具有 NULL 的对应行。我怎么可能只有 select 行带有外键 2 和 3?
foreign_key | created_at
1 12345...
1 12345...
2 12345...
3 12345...
1 NULL
您可以使用 not exists
:
select *
from mytable t
where not exists (
select 1
from mytable t1
where t1.foreign_key = t.foreign_key and t1.created_at is null
)
另一种选择是使用window函数;这是一种使用布尔值 windowing:
的方法select *
from (
select t.*, bool_or(created_at is null) over(partition by foreignkey) has_null
from mytable t
) t
where not has_null