这个问题之前就有遇到过。然后忘记怎么解决了。这次记录一下
这个问题目前发现原因可能有两个。
1、在字段列表上写复杂的sql然后刚好有where条件中使用了ID=XXX然后XXX又等于空的
2、复杂的关联查询用了多层left join 、union all的。这种可能是和字段列表中又参与计算时。与下面left join的顺序有关。例如
select
xxxxxx
(case
when p.decisionId != 0 then (case
when o.orderNo is not null
then aid.orderPrice * dc.exchangeRate / x.cnt
else p.intendingSum / x.cnt end)
else p.intendingSum / x.cnt end) as 订单金额要除于资产数,
xxxxxx
from
xxxxxx
left join (select count(purchaseNo) as cnt, purchaseNo, ordersno
from AssetsInfoDetail
group by purchaseNo, ordersno) x on x.purchaseNo = aid.purchaseno and
((x.ordersno = aid.ordersno and x.ordersno is not null and
aid.ordersno is not null) or
(x.ordersno is null and aid.ordersno is null))
left join Decision dc on dc.id = p.decisionId
xxxxxx
where
xxxxxx
将下面两个left join 调换一下位置就可以正常执行了。
推测可能是由于计算下面代码时dc要比cnt先使用导致?
then aid.orderPrice * dc.exchangeRate / x.cnt
之前有看百度其他资料。说可能与联合查询的表顺序有关