我有一个查询,可以计算收据中的项目并返回其数据以生成报告。我有一个返回某些项目的场景,因此在获取数据时必须通过用return_item_id进行所有操作来考虑到这一点。
项目表:
id
名称
操作表:
return_item_id
数量
ReceiptItem表:
item_id
价格
数量
{{1}}
我想要的预期结果是正确显示所有金额,但是发生的情况是当SUM(cps_operation.amount)返回Null时,因为没有返回操作会返回空值,而不是只显示没有SUM( cps_operation.amount)。
答案 0 :(得分:0)
使用合并
select
cps_item.name as "item.name",
ROW_NUMBER () OVER (ORDER BY e.create_ts) as "row",
e.amount - coalesce(SUM(cps_operation.amount),0) as "amount",
e.price as "price",
e.amount * e.price as "sum"
from cps_receipt_items e
left join cps_item ON e.item_id = cps_item.code
left join cps_operation ON e.id = cps_operation.return_item_id
where e.receipt_id = ${sell}
and e.item_id is not null
group by cps_item.name, e.amount, e.price, e.create_ts;