【发布时间】:2020-09-29 14:42:07
【问题描述】:
我在 Postgres 11 中有下表
id type1 type2 type3 code
NCT00160290 Drug lactulose lactulose A05BA | A06AD
NCT00160290 Drug plantago ovata plantago ovata (null)
NCT00251238 Drug ginkgo biloba extract ginkgo biloba extract (null)
我想提取所有那些 type3 不为空但代码为空的行,但如果任何 id 有任何 type3 的代码,我应该排除它。
我正在尝试以下查询
select distinct *
from table
where type3 is not null
and code is null --but this will include 'NCT00160290' which has a code
group by id
想要的输出是:
id type1 type2 type3 code
NCT00251238 Drug ginkgo biloba extract ginkgo biloba extract (null)
【问题讨论】:
-
不相关,但是:
select distinct *没有意义。*包含主键列,因此永远不会删除任何重复项。 -
您说您不想在输出中出现
'NCT00160290',但它在您想要的结果中,所以您的问题还不清楚。 -
是的,谢谢你的指出。我更正了。
标签: sql postgresql select subquery window-functions