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 >=.
Related
I need your advise to add group by for below piece of code. I tried couple of things but it is giving not a group by function error to me.
Select rec_id,
Case when is_eff=1 and min(date_dt) > dt1 and min(date_dt) <= (dt1 + 90)
Then 'Yes'
Case when is_eff <> 1 and impl_dt is not null and min(impl_dt) > dt1 and min(impl_dt) <= (dt1+90)
Then 'Yes'
Else 'No'
End as column
From Table
Group by rec_id
Error: Not a group by expression
Any suggestions on this please.
You type CASE WHEN THEN, CASE WHEN THEN,...
Syntax is different than what I found on oracle docs:
https://www.oracletutorial.com/oracle-basics/oracle-case/
Being CASE WHEN THEN, WHEN THEN, WHEN THEN,...
Also, fields you are not using aggregate functions on in your CASE should also be included in your group by clause.
Select rec_id,
Case when is_eff=1 and min(date_dt) > dt1 and min(date_dt) <= (dt1 + 90)
Then 'Yes'
when is_eff <> 1 and impl_dt is not null and min(impl_dt) > dt1 and min(impl_dt) <= (dt1+90)
Then 'Yes'
Else 'No'
End as column
From Table
Group by rec_id,dt1,is_eff,impl_dt
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
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.
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?
How can i do something like this ?
SELECT (IF(SUM(Costo) > 0) SUM(Costo) ELSE 0) AS Expr1
FROM TDP_NotaSpeseSezB
You want to use a CASE statement.
Select
CASE WHEN SUM(Costo) > 0 THEN SUM(Costo)
ELSE 0
END 'Expr1'
FROM
TDP_NotaSpeseSezB
You can use case statement like this:
SELECT case when sum(Costo)> 0 then sum(Costo)
else 0 end as Expr1
FROM TDP_NotaSpeseSezB
CASE (Transact-SQL)
SELECT CASE WHEN SUM(Costo) > 0 THEN SUM(Costo) ELSE 0 END AS Expr1
FROM TDP_NotaSpeseSezB
Other major engines support this syntax:
SELECT GREATEST(SUM(Costo), 0)
FROM TDP_NotaSpeseSezB
SQL Server, however, does not.