SELECT item_id,
CREDITBAL= (SELECT Sum(quantity)
FROM axi_quantity_registers
WHERE from_where != 'OPENING'
AND transaction_type = 'C'),
OPENBAL = (SELECT Sum(quantity)
FROM axi_quantity_registers
WHERE from_where = 'OPENING'),
DEBITBAL = (SELECT Sum(quantity)
FROM axi_quantity_registers
WHERE transaction_type = 'D')
FROM axi_quantity_registers
答案 0 :(得分:0)
看起来你想要一个带有条件聚合的GROUP BY
:
SELECT item_id,
sum(case when from_where != 'OPENING' AND transaction_type = 'C' then quantity end) as CREDITBAL,
sum(case when from_where = 'OPENING' then quantity end) as OPENBAL,
sum(case when transaction_type = 'D' then quantity end) as DEBITBAL
FROM axi_quantity_registers
group by item_id