SQL查找没有带值的列的重复行

Ano*_*ous 0 sql

我有一张类似的表

transactionid | ordernumber
aaaa            1
aaaa            NULL
bbbb            2
bbbb            NULL
cccc            NULL
Run Code Online (Sandbox Code Playgroud)

我需要在数据库中找到事务ID没有与之关联的订单号的行.因此,对于此示例,查询应仅返回cccc而不是aaaa或bbbb,因为存在与这些事务关联的订单号.

我尝试了很多东西,但没有运气.我确信这很简单,我只是把它变得过于复杂.显然,如果我进行查询,其中ordernumber为NULL,我仍然会收到可能具有与之关联的ordernumbers的事务.

任何帮助,将不胜感激.

jue*_*n d 6

分组transactionid并总结所有ordernumber不是null.这笔钱必须是0

select transactionid 
from your_table
group by transactionid 
having sum(case when ordernumber is not null then 1 else 0 end) = 0
Run Code Online (Sandbox Code Playgroud)