SQL query using if else - sql

My SQL code looks like this:
SELECT
Scores.PupilId, Scores.BoysName, Scores.FormGroup,
IF (Scores.FormGroup = "10SB", "Great", "ok")
FROM
Scores
I get this message
no such function: if: SELECT Scores.PupilId, Scores.BoysName, Scores.FormGroup,
if(Scores.FormGroup="10SB","Great","ok")
FROM Scores
This is flat file database
Can anyone please help me understand why I am getting a message?

The correct ANSI-standard conditional expression in SQL is the case expression:
SELECT Scores.PupilId, Scores.BoysName, Scores.FormGroup,
(CASE WHEN Scores.FormGroup = '10SB' THEN 'Great' ELSE 'ok' END)
FROM Scores ;

Related

Create multiple case statements in SAP HANA

I'm trying to understand how work the expression in SAP HANA.
I want to create multiple case in one expression.
I have this:
case when (case when "name" = 'NomEntreprise'
then "value"
end) = 'Entreprise Test'
Then 'OK'
end
But when I'm doing this, it doesn't accept the syntax of the code, someone can explain me why ?
case when (case when "name" = 'NomEntreprise'
then "value"
end) = 'Entreprise FAIL'
Then 'FAIL'
end
Thank you for you help

CASE WHEN SQL Syntax Error

I am attempting to us the CASE statement for the first time and I cannot understand why I am getting a syntax error on the last WHEN and ELSE. I am trying to extract a substring if a value starts with a specific set of characters. My Code is below:
SELECT
CASE
WHEN LEFT ([RCode],2) = 'BB' THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT ([RCode],4) = 'APT-' THEN SUBSTRING([RCode],5,LEN([RCode])
WHEN LEFT ([RCode],4) = 'PS-' THEN SUBSTRING([RCode],4,LEN([RCode])
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]
Any guidance would be appreciated.
This won't (necessarily) fix your syntax problem, but I would recommend that you use like for the comparisons:
SELECT (CASE WHEN RCode LIKE 'BB%' THEN SUBSTRING([RCode], 3, LEN([RCode]))
WHEN RCode LIKE 'APT-%' THEN SUBSTRING([RCode], 5, LEN([RCode]))
WHEN RCode LIKE 'PS-%' THEN SUBSTRING([RCode], 4, LEN([RCode]))
ELSE [RCode]
END)
FROM [Xperdyte].[dbo].[tJCLines];
Then you don't have to count characters -- and the third condition will match.
Missed off two right parenthesis.
SELECT CASE WHEN LEFT([RCode],2) = 'BB'
THEN SUBSTRING([RCode],3,LEN([RCode]))
WHEN LEFT([RCode],4) = 'APT-'
THEN SUBSTRING([RCode],5,LEN([RCode]))
WHEN LEFT([RCode],4) = 'PS-'
THEN SUBSTRING([RCode],4,LEN([RCode]))
ELSE [RCode]
END
FROM [Xperdyte].[dbo].[tJCLines]

How can I use CASE expression with SUM function in SQL?

I have an Oracle SQL query that I need to select certain deduction codes and provide a predetermined amount if based on the premium amount for deduction code 91B. These are hard coded values that I need to summarize.
SELECT SUM(DECODE(DEDCD,'91A',AL_AMOUNT,0)) MED91A,
SUM(CASE WHEN DEDCD = '91B' THEN
DECODE(AL_AMOUNT, 23.54,7.85,
40.62,8.31,
43.85,8.31,
56.77,8.31,
AL_AMOUNT)) MED91B
FROM PS_AL_CHK_DED
WHERE WEEK_NBR = 6
AND TO_CHAR(CHECK_DT, 'YYYY')=TO_CHAR(SYSDATE, 'YYYY')
The SUM(DECODE ..) statement used to summarize values where dedcd = '91A' works fine. When I add the part to summarize values where dedcd = '91B' it produces a 'Missing Keyword' error after the decode statement. I'm trying to streamline the query in order to produce the results I need for these two deduction codes because the full query takes entirely too long to run.
Oracle Sql Developer 4.0.2.15
You must complete the case expression syntax with an END keyword.
Do it like this:
CASE WHEN DEDCD = '91B' THEN
DECODE(AL_AMOUNT, 23.54,7.85,
40.62,8.31,
43.85,8.31,
56.77,8.31,
AL_AMOUNT)
END
A basic case expression looks like:
CASE [ expression ]
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
...
WHEN condition_n THEN result_n
ELSE result
END
See the documentation for more details.

If operator in groovy sql select statement

I would like to use the if operator in the select statement, using an executeQuery command.
Something like:
def tmpRec = BankStatement.executeQuery("select SUM(IF(operation='CREDIT',cashAmount,-1*cashAmount)) from BankStatement group by portfolio,currency,code")
go
but the IF operator doesn't work.
thank you
Although I do not know Groovy SQL, in most DBMS the CASE statement is used as a conditional statement. Perhaps the following will work:
def tmpRec = BankStatement.executeQuery("select SUM(CASE WHEN operation='CREDIT' THEN cashAmount ELSE (-1*cashAmount) END) from BankStatement group by portfolio,currency,code")

Using a CASE statement in HQL select

Is there any way to do the following in HQL:
SELECT
case when flag = true then SUM(col1) else SUM(col2)
FROM
myTable
I guess you can (3.6, 4.3) [inline edit] ...for where-clauses:
"Simple" case, case ... when ... then ... else ... end, and "searched" case, case when ... then ... else ... end
Apparently the ability to do this was added in 3.0.4, with the limitation that you cannot use sub-selects in the else clause.
See Hibernate-Forum: https://forum.hibernate.org/viewtopic.php?t=942197
Answer from Team (Gavin):
case is supported in the where clause, but not in the select clause in HB3.
And seen in JIRA with State "Unresolved".
Below you can find a working query (hibernate on postgresql) that uses 2 case statements to replace a boolean value with the corresponding textual representation.
SELECT
CASE ps.open WHEN true THEN 'OPEN'
else 'CLOSED' END,
CASE ps.full WHEN true THEN 'FULL'
else 'FREE' END,
ps.availableCapacity
FROM ParkingState as ps
I facing the same problem in HQL then I solved the following query is
select CONCAT(event.address1,', ', CASE WHEN event.address2 IS NULL THEN '' ELSE concat(event.address2,', ') END, event.city from EventDetail event where event.startDate>=:startDate and event.endDate<=:endDate;
We use hibernate HQL query extensively and I think finally there is a hackish way of doing such a thing :
Assuming we originally had a query of
i2.element.id = :someId
Then decided to expand this to be something like this:
((i.element.id = :someId and i2.element.id=:someId) or (i2.element.id = :someId))
But there was an issue where we want it to only lookup for this based on classType so a case statement:
(case when type(i)=Item then
((i.element.id = :someId and i2.element.id=:someId) or (i2.element.id = :someId))
else
i.element.id = :someId
end)
Above will not work you could make an easy version of above work by doing:
(case when type(i)=Item then
i2.element.id
else
i.element.id
end)=:elementId
But this does not actually do what we need it to do, we want it to do exact above query, so knowing you can assign a variable at the end of a case statement in there where bit of HQL:
(
(
(case when
type(r)=Item then
i.element.id
else
i.element.id end) = :elementId
and
(case when
type(r)=Item then
i2.element.id
else
i.element.id end) = :elementId
)
or
(case when
type(r)=Item then
i2.element.id
else
i.element.id end) = :elementId
)
I have managed to make the query now work based on case statement, sure it is a lot more long winded but actually does the same as the first instance
This is an example using a string comparison in the condition:
SELECT CASE f.type WHEN 'REMOVE'
THEN f.previousLocation
ELSE f.currentLocation
END
FROM FileOperation f