首页 > 解决方案 > 检查包含所有空值的字段的多行事务

问题描述

我有一个事务表,其中完整的事务(tranid)根据下面的示例跨越多行

交易示例

我要查找的是包含所有空值与仅部分具有空值的 tranid 的事务,并且仅显示 tranid 在 acount_id 下每列为空的情况,而在上面的示例中仅显示 6 个中的 2 个。

我编写了以下查询,但搜索“is null”只会产生第一笔交易中包含 null 的两行,而不是在整个交易中搜索所有 null。我不确定如何搜索,希望有人可以帮助解决这里的逻辑。

select * 
from ns.tItemFulfillment itf 
    inner join ns.tsalesorder so on itf.created_from_id = so.transaction_id
    left join ns.tpurchaseorder tpo on so.transaction_id = tpo.created_from_id
    inner join ns.transaction_lines tl on itf.transaction_id = tl.transaction_id
    left join ns.accounts a on tl.account_id = a.account_id
where tl.account_ID IS NULL and tpo.created_from_id is null 

产生以下内容,如您所见,这只是上面事务的最后两行。

在此处输入图像描述

我应该只能查询带有所有空值的 tranid,这是我需要帮助的。谢谢

标签: sqlsql-server

解决方案


使用聚合:

select tranid
from mytable —- or whatever join you need
group by tranid
having max(account_id) is null
and max(account_number) is null
—- etc for all columns that need to be all null

推荐阅读