SQL SUM with Where Condition - sql

sum(raceprice) / sum(bidding_price) * sum( bidding_price Where bidding_type = 'small')
- bet_price as Totla_profit
I keep getting error message about that sum condition, don't know what is wrong with it, please help.

Replace
sum(bidding_price Where bidding_type = 'small')
with
sum(case when bidding_type = 'small'
then bidding_price
else 0
end)

Related

SQL using SUM in CASE in SUM

I had this query select
sum(CASE WHEN kpi.average >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
which worked fine, but I had to change it to this
sum(CASE WHEN sum(kpi.averageUse) / sum(kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
These queries have to get number of rows, where some value (average) is greater than average from TEMP table. But in the second query I have more accurate data (weighted average).
but I am getting error
1111 invalid use of group function
Any ideas how to write SUM in CASE in SUM?
Thanks!
This code is just non-sensical because you have nested sum functions:
sum(CASE WHEN sum(kpi.averageUse) / sum(kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,
Without seeing your larger query, it is not possible to know what you really intend. But I would speculate that you don't want the internal sum()s:
sum(CASE WHEN (skpi.averageUse / kpi.averageTotal) >= temp.average THEN 1 ELSE 0 END) AS recordOrder,

SQL Cast, Case and Count function

I am trying to count rows of email opens and divide it by the total email sends and convert this into decimal (so it shows percentage).
I am getting the error: "An error occurred while checking the query syntax. Errors: Incorrect syntax near ')'."
I have tried separating the code so I can get the count and case function separately to cast. Cast works without the count/case. Count/case also works independently, it just doesn't seem to work all together. Am I missing something here?
SELECT
SubscriberKey
,CAST(
((COUNT(CASE
WHEN PreviousMonth <= 1
AND SendType = 'Auto'
AND Opened = 1
THEN 1
ELSE NULLEND ))/
(COUNT(CASE
WHEN PreviousMonth <= 1
AND SendType = 'Auto')))
AS DECIMAL(18,4)) * 100 AS 'AverageOpen'
FROM Data
GROUP BY SubscriberKey
An error occurred while checking the query syntax. Errors: Incorrect syntax near ')'.
I would write this using SUM(CASE). Note two things:
The 1.0 so the division is not integers (which can be an issue in some databases.
No else in the denominator to avoid divide-by-zero
I also left out the cast():
SELECT SubscriberKey
(SUM(CASE WHEN PreviousMonth <= 1 AND
SendType = 'Auto'
Opened = 1
THEN 1.0 ELSE 0
END) /
SUM(CASE WHEN PreviousMonth <= 1 AND
SendType = 'Auto'
THEN 1 -- no else so no divide by 0
END)
) * 100 AverageOpen
FROM Data
GROUP BY SubscriberKey;
You code may be failing because of NULLEND is not recognized in SQL.
I think what you want to achieve here is to get the average of opened = 1 divided by all.
SELECT cast((t1.Opened/t1.OverAll) as decimal(18,4)) * 100.00 as 'AverageOpen'
FROM
(SELECT
SubscriberKey
, SUM(CASE WHEN PreviousMonth <= 1 AND SendType = 'Auto' AND Opened = 1 THEN 1 ELSE 0 END) Opened,
, SUM(CASE WHEN PreviousMonth <= 1 AND SendType = 'Auto' THEN 1 ELSE 0 END) OverAll
FROM Data
GROUP BY SubscriberKey) as t1

SQL statement selecting a count of a column

I have this SQL query:
SELECT
COUNT(SDDOCO) AS Total
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
which returns a count of all the orders for today.
I am trying to get a second column so that I have this:
To get orders that are not completed would be: SDNXTR < '562'. How would I add this to my sql query so that I can accomplish this goal? Let me know if you need anymore information and thanks in advance for your responses.
You have two options here:
SELECT
COUNT(SDDOCO) AS Total,
SUM(CASE WHEN SDNXTR < '562' THEN 1 ELSE 0 END) AS Not_Finished_Yet_With_SUM,
COUNT(CASE WHEN SDNXTR < '562' THEN 1 ELSE NULL END) AS Not_Finished_Yet_With_COUNT,
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
You can use case statement to count that SDNXTR < '562' values like below:
SELECT count(SDDOCO) as Total,
sum(case when SDNXTR < '562' then 1 else 0 end) as not_yet_finished
FROM KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE SDDRQJ = Day10

SQL syntax error in case statement inside sum function

Below is my code:
SELECT HOUR(asl.Start_Time) AS [Hour],
t.team_name,
SUM( CASE WHEN asl.Working = 1
THEN asl.Duration_Seconds
ELSE 0 END)
AS TotalSeconds
FROM (Agents
INNER JOIN Teams AS t
ON Agents.team_no = Teams.team_no)
INNER JOIN AgentStateLogs AS asl
ON Agents.agent_no = asl.Agent_No
GROUP BY t.team_name, HOUR(asl.Start_Time);
The problem is that it is saying there is a missing operator in the statement:
SUM( CASE WHEN asl.Working = 1
THEN asl.Duration_Seconds
ELSE 0 END)
AS TotalSeconds
Ive been trying to find it and I just cant see where the error lies. Can anyone shed some light on this for me? This code is being done in MS Access 2013.
Thank you in advance.
CASE WHEN asl.Working = 1
THEN asl.Duration_Seconds
ELSE 0 END
Translated to JET, this becomes
IIf(asl.Working = 1, asl.Duration_Seconds, 0)
Search for immediate if in the manual for more information.

Need to solve Group by

SELECT WK_NBR,region_cd,plant_cd, POS_GAP_1DAY
FROM(SELECT wk_nbr,region_cd,plant_cd,
round((CASE WHEN SUM(gap_1day)>0 THEN SUM(gap_1day) ELSE 0 END ),2) AS POS_GAP_1DAY
FROM
table a
group by wk_nbr,region_cd,plant_cd)
In the above query the problem I am facing is POS_GAP_1DAY should sum up only the positive numbers in the column but the above query will not give the right answer since I check SUM(gap_1day)>0.
The problem is I need to group by only wk_nbr,region_cd,plant_cd.
Please suggest
Try change
round((CASE WHEN SUM(gap_1day)>0 THEN SUM(gap_1day) ELSE 0 END ),2) AS POS_GAP_1DAY
to
ROUND(SUM(CASE WHEN SIGN(gap_1day) > 0 THEN gap_1day ELSE 0 END), 2) AS POS_GAP_1DAY
done on the spot, so I don't know if it works, but...
SELECT wk_nbr,region_cd,plant_cd,
round(SUM(CASE WHEN GAP_1DAY>0 then GAP_1DAY else 0 end),2) AS POS_GAP_1DAY
FROM
table a
group by wk_nbr,region_cd,plant_cd
doesn't do what you want?