SQL How to check if field value is >0 (case when)? - sql

I try to do an aggregation with two fields but only if the value of one field is not 0.
SELECT
CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION / CC90_StatisticQueueData.SUM_WAITING_DURATION)
FROM
CC90_StatisticQueueData
I always receive **ERROR**:
Incorrect syntax near the keyword 'FROM'.`
and don't know how to fix it.
Thanks in advance.

Use the END Keyword in CASE Statement :
SELECT CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION /
CC90_StatisticQueueData.SUM_WAITING_DURATION) END
FROM CC90_StatisticQueueData

Related

Calculated column incorrect syntax

I want to create a calculated column in SQL Server 2017 Developer as
ISNULL([Nominator]/NULLIF([Denominator]), 0) AS [Recidivation]
but this raises an error
Incorrect syntax near '('.
This syntax works:
[Nominator]/[Denominator] AS [Recidivation]
I must be blind or something but have somebody any idea how I can't prevent division by zero in a calculated column as shown above?
Help would be appreciated. Thanks.
You need an additional , 0. I also prefer the standard COALESCE() function. So:
COALESCE(Nominator / NULLIF(Denominator, 0), 0) AS [Recidivation]
I also think the intention might be clearer with case:
(CASE WHEN Denominator = 0 or Denominator IS NULL THEN 0
ELSE Nominator / Denominator
END)

Hive - SELECT inside WHEN clause of CASE function gives an error

I am trying to write a query in Hive with a Case statement in which the condition depends on one of the values in the current row (whether or not it is equal to its predecessor). I want to evaluate it on the fly, this way, therefore requiring a nested query, not by making it another column first and comparing 2 columns. (I was able to do the latter, but that's really second-best). Does anyone know how to make this work?
Thanks.
My query:
SELECT * ,
CASE
WHEN
(SELECT lag(field_with_duplicates,1) over (order by field_with_duplicates) FROM my_table b
WHERE b.id=a.id) = a.field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
FROM my_table a
Error:
java.sql.SQLException: org.apache.spark.sql.AnalysisException: cannot recognize input near 'SELECT' 'lag' '(' in expression specification; line 4 pos 9
Notes:
The reason I needed the complicated 'lag' function is that the unique Id's in the table are not consecutive, but I don't think that's where it's at: I tested by substituting another simpler inner query and got the same error message.
Speaking of 'duplicates', I did search on this issue before posting, but the only SELECT's inside CASE's I found were in the THEN statement, and if that works the same, it suggests mine should work too.
You do not need the subquery inside CASE:
SELECT a.* ,
CASE
WHEN prev_field_with_duplicates = field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
FROM (select a.*,
lag(field_with_duplicates,1) over (order by field_with_duplicates) as prev_field_with_duplicates
from my_table a
)a
or even you can use lag() inside CASE instead without subquery at all (I'm not sure if it will work in all Hive versions ):
CASE
WHEN lag(field_with_duplicates,1) over (order by field_with_duplicates) = field_with_duplicates
THEN “Duplicate”
ELSE “”
END as Duplicate_Indicator
Thanks to #MatBailie for the answer in his comment. Don't I feel silly...
Resolved

Correct Syntax for this SQL CASE?

I am new to SQL plus . Could anyone help me figure out the syntax error for my code?
CREATE OR REPLACE VIEW BR_STATUS AS
SELECT CARTS_PER_CUSTOMER.loginName,CARTS_PER_CUSTOMER.number_of_carts,
CASE WHEN (number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS 'BR-status'
FROM CARTS_PER_CUSTOMER;
Whenever I try to run this part of the code, I get this error message
ORA-00923: FROM keyword not found where expected.
I followed several oracle documentation for CASE, but can't figure out what I am writing wrong. Any suggestions would be appreciated.
There is a unnecessary comma before FROM clause nothing wrong with CASE statement. Also use double quotes for Alias name instead of single quotes (thanks to Jarlh)
SELECT CARTS_PER_CUSTOMER.loginName,
CARTS_PER_CUSTOMER.number_of_carts,
CASE
WHEN ( number_of_carts < 1 ) THEN 'BR-1 Satisfied.'
ELSE 'BR-2 violated.'
END AS "BR-status" --Remove the comma here
FROM CARTS_PER_CUSTOMER;

An expression of non-boolean type

With this:
select puser_id, puser_name, plast_login_time, plicense_level =
case
when '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time
I have error like this:
An expression of non-boolean type specified in a context where a
condition is expected, near 'then'.
Please help me to find the problem in code.
Change your WHEN to:
when plicense_level = '1'
You need to have a valid comparison in WHEN to avoid the error.
From the CASE documentation:
WHEN when_expression
Is a simple expression to which input_expression is compared when the
simple CASE format is used. when_expression is any valid expression.
Your syntax for the CASE statement expression isn't quite right, try:
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end
If what you're trying is to show the user a text describing the licennse instead of the numeric value of the column you should try something like this:
select
puser_id,
puser_name,
plast_login_time,
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end as plicense_level
from dbo.PPOM_USER
order by plast_login_time
You can find some samples of CASE in this page at MSDN
Thank all of you!
The dicision was in changing data type of field plicense_level in report
Figure 1
Figure 2
select puser_id, puser_name, plast_login_time, plicense_level =
case
when plicense_level = '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time

call a scalar value function in stored procedure based on a condition

Hi am using a scalar function to compute some values and add it as a column while retriving the table.The query i was using is
Select * ,dbo.funcnme(#userid,itemid) as newvalue from items
now i have a scenario in which i have to call a function based on a items value but i cant get it right.the query i tried is
select *,
Case when items.Value=4
then dbo.funcnme(#userid,itemid) as newvalue
else dbo.newfuncnme(#userid,itemid)
as newvalue from items
it shows
Incorrect syntax near the keyword 'as'.
Whats wrong here.How can i do it please help.
I'm missing the END of the CASE, apart from that the alias should also be at the end:
select *,
Case when items.Value=4
then dbo.funcnme(#userid,itemid)
else dbo.newfuncnme(#userid,itemid)
END as newvalue
from items