0
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

4

3 回答 3

3

您正在以正确的方式进行操作。

请参阅此示例

你只是犯了一个小错误。您只需要END在此之后添加如下条件:

SUM(CASE WHEN DrCrType = 'DR' 
          and VoucherDate <= '2012-04-01' 
         THEN Amount 
         ELSE (Amount*-1) END) AS opening_sum
                          ^^^

所以你的整个查询应该是这样的:

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) END) 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
于 2013-04-19T05:36:50.537 回答
1
 WHEN PAT_DrCrType = 'DR' and VoucherDate <= '2012-04-01' THEN Amount ELSE (Amount-1)) AS opening_sum
于 2013-04-19T05:35:41.613 回答
1

试试这个方法

SUM(CASE WHEN DrCrType = 'DR' AND VoucherDate <= '2012-04-01' THEN Amount ELSE (Amount-1) END)
于 2013-04-19T05:38:22.207 回答