SELECT query with CASE condition and SUM()

前端 未结 5 2310
遥遥无期
遥遥无期 2020-12-03 02:59

I\'m currently using these sql statements. My table has the field CPaymentType which contains \"Cash\" or \"Check\". I can sum up the amount of payments by executing 2 SQL s

5条回答
  •  萌比男神i
    2020-12-03 03:23

    I don't think you need a case statement. You just need to update your where clause and make sure you have correct parentheses to group the clauses.

    SELECT Sum(CAMount) as PaymentAmount 
    from TableOrderPayment 
    where (CStatus = 'Active' AND CPaymentType = 'Cash') 
    OR (CStatus = 'Active' and CPaymentType = 'Check' and CDate<=SYSDATETIME())
    

    The answers posted before mine assume that CDate<=SYSDATETIME() is also appropriate for Cash payment type as well. I think I split mine out so it only looks for that clause for check payments.

提交回复
热议问题