postgresql: SUM with OR operator with an aggregation query - sql

I have successfully done following query:
SELECT rs.personid, rs.rec_count,
sum(case when r.rectype = 'degreeOne' then 1 else 0 end)
but now I want to change this to following
SELECT rs.personid, rs.rec_count,
sum(case when r.rectype = 'test1' then 1 else 0 end) OR sum(case when r.rectype = 'random' and r.badgeid = 'bronze' then 1 else 0 end) as degree_one, ...
basically I want to do sum with an "OR" operator, I tried as I have shown but it gives me an error.

sum (
case
when
r.rectype = 'test1'
or
r.rectype = 'random' and r.badgeid = 'bronze'
then 1
else 0
end
) as degree_one

Related

Why doesn't the "having" clause work?

For the following query:
select count(distinct email_address)
from
(
select distinct email_address,
case when elq_activity_type='EmailSend' then 1 else 0 end 'Sends',
case when elq_activity_type='Bounceback' then 1 else 0 end 'Bounces',
case when elq_activity_type='EmailOpen' then 1 else 0 end 'Opens',
case when elq_activity_type='EmailClickthrough' then 1 else 0 end 'Clicks'
from elq_stg_activity
) a
having sum(sends-bounces)>0
The having clause doesn't seem to be doing anything. What am I doing wrong?
Need to get all unique emails that had an email delivered to them (send-bounce).
Thanks!
I think you want this:
select count(email_address)
from (select email_address,
sum(case when elq_activity_type = 'EmailSend' then 1 else 0 end) Sends,
sum(case when elq_activity_type = 'Bounceback' then 1 else 0 end) as Bounces,
sum(case when elq_activity_type = 'EmailOpen' then 1 else 0 end) as Opens,
sum(case when elq_activity_type = 'EmailClickthrough' then 1 else 0 end) as Clicks
from elq_stg_activity
group by email_address
) a
where sends = bounces;
There are numerous issues with your query. This is the only sensible interpretation I could think of.

How Do I Use Case When with Group By statement

I have made a query but it don't give me what i want :
SELECT Designation_projet, COUNT(*) AS [Nb Demande], CASE WHEN Validation = 0 THEN COUNT(*) END AS Validée, CASE WHEN Validation = 1 THEN COUNT(*)
END AS NonValidée, CASE WHEN Commandé = 0 THEN COUNT(*) END AS NonCommandé,
CASE WHEN Commandé <> 0 THEN COUNT(*) END AS Commandé, SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet,Commandé,Validation
Please help me to achieve my gool.
Thanks in advance
You can do this using condition aggregation. That means that the CASE expression is the argument to SUM() -- it goes "inside" not "outside":
SELECT Designation_projet, COUNT(*) AS [Nb Demande],
SUM(CASE WHEN Validation = 0 THEN 1 ELSE 0 END) as Validée,
SUM(CASE WHEN Validation = 1 THEN 1 ELSE 0 END) as NonValidée,
SUM(CASE WHEN Commandé = 0 THEN 1 ELSE 0 END) AS NonCommandé,
SUM(CASE WHEN Commandé <> 0 THEN 1 ELSE 0 END) END AS Commandé,
SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet;
If you want one row per Designation_projet, then that should be the only key in the GROUP BY.
Assuming Validation takes on only two values, you can simplify the first two aggregation expressions:
SELECT Designation_projet, COUNT(*) AS [Nb Demande],
SUM(1 - Validation) as Validée,
SUM(Validation = 1) as NonValidée,
SUM(CASE WHEN Commandé = 0 THEN 1 ELSE 0 END) AS NonCommandé,
SUM(CASE WHEN Commandé <> 0 THEN 1 ELSE 0 END) END AS Commandé,
SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet;
You may be able to simplify the Commandé expressions as well.
Try this
SELECT Designation_projet, COUNT(*) AS [Nb Demande],sum(CASE WHEN Validation = 1 THEN 1 else 0 END AS Validée,sum( CASE WHEN Validation = 1 THEN 1 else 0
END AS NonValidée, sum(CASE WHEN NonCommandé <> 1 THEN 1 else 0 END AS NonCommandé,
sum(CASE WHEN Commandé <> 1 THEN 1 else 0 END AS Commandé, SUM(TotalHT) AS TotalHT
FROM V_DemandeAchat
GROUP BY Designation_projet

SQL percentage with rows same table with different where condition

I want to do a query like:
select
count(asterisk) where acción='a'/count(asterisk) where acción='b' * 100
from
same_table
grouped by day
but I don't want use subquery, is it possible with joins?
I`m not sure the syntax is correct, but you can use something like this:
SELECT day,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A,
SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) AS SUM_B,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A / SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) * 100 AS result
FROM your_table
GROUP BY day
The concept is to actually sum the the values that you need, instead of count.

Avoiding a divide by zero error in a case statement

I am getting a divide by zero error in my script.
Can anyone please help.
I am trying to divide two records and one of them has zero in it. I dont want to lose the row, please advise.
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) as "Paid in 24hrs",
COUNT([MONTH_OF_SUSPENSION])/sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) as "Achieved"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
You can introduce a second case to check the result of the sum:
case
when sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end) > 0
then COUNT([MONTH_OF_SUSPENSION])/sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end)
else 0 /* a default value that makes sense to you */
end as "Achieved"
Looking at your code I could assume you are using MSSQL and therefore you could use nullif which returns null if two arguments are equal. So for example your code could look like :
COUNT([MONTH_OF_SUSPENSION])/nullif(sum(case when [PAYMENT_<=24HRS] = 'Y' then 1 else 0 end),0) as "Achieved"
What it does is if the value of the sum operator is equal 0 then the divisor is turn from zero into null and that will result in the entire equation to become null.
use another case statement to check the result of your sum
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "Paid in 24hrs",
case
when sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) = 0 then 'whatever you want in this case'
else COUNT([MONTH_OF_SUSPENSION])/sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "Achieved"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
although this is pretty nasty looking, so you could tidy it up a bit with a sub-select:
select
year,
month,
request,
PaidIn24hrs,
case
when PaidIn24hrs = 0 then 'whatever you want in this case'
else request/PaidIn24hrs
end as "Achieved"
from
(
select DATEPART(Year,Request_date) as "Year",
DATEPART(Month,Request_date) as "Month",
COUNT([MONTH_OF_SUSPENSION]) as "Request" ,
sum(case
when [PAYMENT_<=24HRS] = 'Y' then 1
else 0
end) as "PaidIn24hrs"
FROM suspension_br
where REQUEST_STATUS = 'OTHERS'
GROUP BY DATEPART(Year,Request_date),DATEPART(Month,Request_date)
)

How do you get the product of two case statements

I am trying to combine case statements using the following code and keep encountering errors.
SELECT
dbo.COL_TBL_WAGES.[Job Group],
SUM(CASE
WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST'
THEN ([CountOfEMP_TNG_STT_DT] *
(WHEN IsNumeric([TBL_VCOURSE.Length])
THEN TBL_VCOURSE ELSE 0 END) AS ST_Hours
It looks like you want this since you have two CASE expressions in your original:
SELECT dbo.COL_TBL_WAGES.[Job Group],
SUM(CASE
WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST'
THEN [CountOfEMP_TNG_STT_DT]
ELSE 0 END
* CASE
WHEN IsNumeric([TBL_VCOURSE.Length]) = 1
THEN TBL_VCOURSE
ELSE 0 END
) AS ST_Hours
Edit, based on your comment your query will be similar to this:
SELECT dbo.COL_TBL_WAGES.[Job Group],
SUM(CASE
WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST'
THEN [CountOfEMP_TNG_STT_DT] ELSE 0 END
* CASE
WHEN IsNumeric([COL_TBL_VCOURSE].[Length]) = 1
THEN COL_TBL_VCOURSE.[LENGTH]
ELSE 0 END) AS ST_Hours,
SUM(CASE
WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST'
THEN [CountOfEMP_TNG_STT_DT]*CAST([2011 Total] AS FLOAT) ELSE 0 END
* CASE
WHEN IsNumeric([COL_TBL_VCOURSE].[LENGTH]) = 1
THEN CAST([COL_TBL_VCOURSE].[LENGTH] AS FLOAT) ELSE 0 END) AS ST_Cost