我有这样的疑问:
select nvl(nvl(sum(a.quantity),0)-nvl(cc.quantityCor,0),0)
from RCV_TRANSACTIONS a
LEFT JOIN (select c.shipment_line_id,c.oe_order_line_id,nvl(sum(c.quantity),0) quantityCor
from RCV_TRANSACTIONS c
where c.TRANSACTION_TYPE='CORRECT'
group by c.shipment_line_id,c.oe_order_line_id) cc on (a.shipment_line_id=cc.shipment_line_id and a.shipment_line_id=7085740)
where a.transaction_type='DELIVER'
and a.shipment_line_id=7085740
group by nvl(cc.quantityCor,0);查询运行正常,但不返回值。如果没有找到数量,我希望它返回0。我哪里出错了?
发布于 2019-08-22 20:28:15
如果筛选出所有行,则带有GROUP BY的聚合查询不返回任何行。
没有GROUP BY的聚合查询总是返回一行,即使所有行都被过滤掉。
所以,只需删除GROUP BY即可。并将SELECT更改为:
select coalesce(sum(a.quantity), 0) - coalesce(max(cc.quantityCor), 0) 发布于 2019-08-22 20:40:41
我可能错了,但看来你们只是想从7085740期装运的交货数量中减去正确的数量。您不需要对此进行复杂的查询。尤其是你的GROUP BY条款,如果这是你想要的,那就没有意义了。
编写此查询的一种方法是:
select
sum(case when transaction_type = 'DELIVER' then quantity else 0 end) -
sum(case when transaction_type = 'CORRECT' then quantity else 0 end) as diff
from rcv_transactions
where shipment_line_id = 7085740;发布于 2019-09-06 23:07:03
我有这样的查询,当该项无效时,我试图返回'X‘。
SELECT case when segment1 is not null then segment1 else 'X' end
--INTO v_orgValidItem
FROM mtl_system_items_b
WHERE segment1='1676001000'--'Jul-00'--l_item
and organization_id=168;..but --它返回空。
更改为使用不带组的聚合,现在它在项无效时返回'X‘。
SELECT case when max(segment1) is not null then max(segment1) else 'X' end valid
--INTO v_orgValidItem
FROM mtl_system_items_b
WHERE segment1='1676001000'--'Jul-00'--l_item
and organization_id=168;--l_ship_to_organization_id_pb;https://stackoverflow.com/questions/57616602
复制相似问题