SELECT query with CASE condition and SUM()

199,040

Solution 1

Select SUM(CASE When CPayment='Cash' Then CAmount Else 0 End ) as CashPaymentAmount,
       SUM(CASE When CPayment='Check' Then CAmount Else 0 End ) as CheckPaymentAmount
from TableOrderPayment
Where ( CPayment='Cash' Or CPayment='Check' ) AND CDate<=SYSDATETIME() and CStatus='Active';

Solution 2

select CPaymentType, sum(CAmount)
from TableOrderPayment
where (CPaymentType = 'Cash' and CStatus = 'Active')
or (CPaymentType = 'Check' and CDate <= bsysdatetime() abd CStatus = 'Active')
group by CPaymentType

Cheers -

Solution 3

To get each sum in a separate column:

Select SUM(IF(CPaymentType='Check', CAmount, 0)) as PaymentAmountCheck,
       SUM(IF(CPaymentType='Cash', CAmount, 0)) as PaymentAmountCash
from TableOrderPayment
where CPaymentType IN ('Check','Cash') 
and CDate<=SYSDATETIME() 
and CStatus='Active';
Share:
199,040
chris_techno25
Author by

chris_techno25

I love reading about technology so much. It makes me alive. I have been doing Microcontroller projects, software stuff and electronics. I have experience, but no way an expert, in .NET programming (ASP.NET, Winforms, WPF), Android Java and Javascript programming, PHP, SQL, C language, LUA language, TI launchpad MCU, Arduino products, robotics and automation :) I still have much to learn, but I won't stop learning.

Updated on August 04, 2020

Comments

  • chris_techno25
    chris_techno25 almost 4 years

    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 statements as shown below. In this case, the user won't even notice the speed difference when executing 2 sql statements or just 1, however, I don't like my way, I just want 1 sql statement. How do I reconstruct these into 1 statement with CASE conditions? I can't figure it out since examples online result in either 1 or 0 or boolean. I don't want the postdated Check payments to be included. Thank you very much.

    Select SUM(CAmount) as PaymentAmount 
    from TableOrderPayment 
    where CPaymentType='Cash' and CStatus='Active';
    
    Select SUM(CAmount) as PaymentAmount 
    from TableOrderPayment 
    where CPaymentType='Check' and CDate<=SYSDATETIME() and CStatus='Active';
    
  • chris_techno25
    chris_techno25 over 10 years
    Thank you for all your answers, all are correct, but I'd choose this 1 since I was looking for something with the CASE statement. I've never used the CASE statement which is why I want to try this example. I just made 2 sql statements so my point would easily be understood. Thank you Sir!
  • Bleeding Fingers
    Bleeding Fingers over 10 years
    @chris_techno25 In regards to this I suggest, since you are a new user, you go through Does this amount to inconsistency in the edit review process? and Clarification of edit rejection, Meta Stack Overflow discussions.
  • Saraz
    Saraz over 2 years
    Correct one if we want to use CASE statement. Thank you for making our results correct @mudassir-hasan