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.
Related
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
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
I am receiving the message "Error: Encountered "" at line 0, column 0." from google BigQuery from the following abbreviated query;
select b.description, c.description, c.product_key, c.brand, c.u_size,
c.flavor,
sum(case when a.period_key=201611 then dollars else 0 end),
sum(case when a.period_key=201610 then dollars else 0 end),
sum(case when a.period_key=201511 then dollars else 0 end)
from [SN_DM.facts_q] as a join [SN_DM.nb_markets] as b on (a.market_key=b.market_key) join [SN_DM.nb_products]
as c (a.product_key=c.product_key)
where
a.market_key in (55,61,62,63,64,65,66,67,68)
and ( c.category_tag in ('BCHAIR','BCSOAP','BCDEOD') or
c.subcategory_tag in ('BCSKBL','BCSKFC','BCSKFL','BCSKSC','BCSKST'))
and a.period_key between 201412 and 201611
group by b.description, c.description, c.product_key , c.brand, c.u_size, c.flavor ;
I don't see any issues with the SQL and the google message is not useful, Any help would be greatly appreciated.
I encountered a similar issue.
There is something going on with the second join clause.
join [SN_DM.nb_products]
as c (a.product_key=c.product_key)
The "on" term is missing after "c".
Hope it helps.
I need some help with case statement.
my code:
select c.name, c.date_entered, i.transaction, i.planned_date,
case (c.date_entered - i.planned_date)
when (c.date_entered - i.planned_date) > 0 then 'YES'
when (c.date_entered - i.planned_date) < 0 then 'NO'
end RESPECT
from company c,transaction i
but it give me error ORA-00907; missing right parenthesis
thank you to help me with.
select c.name, c.date_entered, i.transaction, i.planned_date,
case when c.date_entered - i.planned_date > 0
then 'YES'
when c.date_entered - i.planned_date < 0
then 'NO'
end as RESPECT
from company c, transaction i
You should also think about the output of when c.date_entered - i.planned_date = 0 or change one of the cases to <= or >=.
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)