select cust_code,occu_name
, SUM(CASE WHEN VoucherType = 'S' THEN Amount ELSE 0 END) AS salesSum
,SUM(CASE WHEN VoucherType = 'I' THEN Amount ELSE 0 END) AS interestSum
,SUM(CASE WHEN VoucherType = '' THEN Amount ELSE 0 END) AS interest_sum
,SUM(CASE WHEN VoucherType = 'P' THEN Amount ELSE 0 END) AS chequereturn_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'R' THEN Amount ELSE 0 END) AS receipt_sum
,SUM(CASE WHEN VoucherType = 'N' THEN Amount ELSE 0 END) AS credit_sum
,SUM(CASE WHEN (DrCrType = 'DR' and VoucherDate <= '2012-04-01') THEN Amount ELSE (Amount*-1)) AS opening_sum
from bmwregistration, ledger_transactions
where bmwregistration.ledger_id = ledger_transactions.OccupierID and
VoucherDate >= '2012-04-01' and
VoucherDate <= '2013-02-01'
group by cust_code
如何检查 sum 函数中的两个条件SUM(CASE WHEN (DrCrType = 'DR' and VoucherDate <= '2012-04-01') THEN Amount ELSE (Amount*-1)) AS opening_sum