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';
Answer from Mudassir Hasan on Stack OverflowSelect 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';
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 -
sql server - SUM (CASE WHEN) AS.. query - Database Administrators Stack Exchange
group by - Sum(Case When) Question - Database Administrators Stack Exchange
Conditional Sum Syntax Are these two methods both correct or do they calculate or return results differently?
Using SUM() In CASE Statement / Best Practices – SQLServerCentral Forums
Trying to find different ways to find the number of male customers, the first way
SELECT COUNT(*) AS num_studend_male FROM performance WHERE gender = 'male';
and the second way.
SELECT SUM(
CASE
WHEN (gender = 'male') THEN 1
ELSE 0
END
) AS num_studend_male
FROM performance;In my second query, how could I count females as well, and add it as a separate column? Is this even possible?
Hello everyone, I am facing an issue, I have a division in query. Something like rate = value/ total value (which is sum). It was working fine but I recently faced error of Zero division. So how can I overcome that? Currently, what I am thinking of doing to avoid 0 in denominator is to use CASE WHEN.
SELECT value/SUM( CASE WHEN total_value = 0 THEN 1 ELSE total_value END) FROM.....
Is it okay to do like this or is there any other approach?
TIA.
Trying to figure out how to sum a case when formula for multiple items -
CASE WHEN {item}
= 'CITL' THEN {amount}
= 'CITV' THEN {amount}
= 'CRDD' THEN {amount}
= 'DETN' THEN {amount}
= 'DLVAP' THEN {amount}
= 'DRASF' THEN {amount}
= 'DRYR' THEN {amount}
= 'LAYVR' THEN {amount}
= 'PGUAR' THEN {amount}
= 'LUMP' THEN {amount}
= 'RCOG' THEN {amount}
= 'RDEL' THEN {amount}
ELSE 0
END
This gets me an error, not sure if I messed this up.
CASE WHEN {item} IN('CITL','CITV','CRDD','etc'....) THEN {amount} ELSE 0 END
OR
CASE WHEN {item} = 'CITL' THEN {amount} WHEN {item} = 'CITV' THEN {amount} WHEN {item} = 'CRDD' THEN {amount} etc ELSE 0 END
The first is cleaner but the second works too. Cheers!
There is also DECODE but IN is most efficient for this situation where they're all the same.
Hello everyone,
I have 3 CASE statements and I am trying to understand how to properly add them without CTE (because it will make the whole code a lot more complex). Does someone know how it can be done?
--CASE WHEN S1FRAC != '0' THEN '1' ELSE '0' END AS 'SORT_ORDER1'
--CASE WHEN MASTER_BOL_NUMBER = '' OR MASTER_BOL_NUMBER = '.' THEN '0' WHEN MASTER_BOL_P8= AR_ORD THEN '1' ELSE '0' THEN 'SORT_ORDER2'
--CASE WHEN MASTER_BOL_NUMBER != '' OR MASTER_BOL_NUMBER= '.' THEN '0' WHEN AR_LOAD = '0' THEN '0' ELSE AR_ORD = L3BOL_P8 THEN '1' ELSE '0' END AS 'SORT_ORDER3'
-- CASE WHEN SORT_ORDER1 + SORT_ORDER2 + SORT_ORDER3 != 0 THEN 1 ELSE 0 AS 'SORT_ORDER'
You can wrap each case statement with SUM but make sure you aren’t trying to sum results that are text. Notice I don’t have single quotes around the 1’s or 0’s.
Example:
SUM(CASE WHEN S1FRAC <> 0 THEN 1 ELSE 0 END) + SUM(CASE WHEN MASTER_BOL_P8= AR_ORD THEN 1 ELSE 0 END)