Create multiple case statements in SAP HANA - sql

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

Related

I am working in datastage and I am trying to input data from one column to another

I used the following statement below:
Trim(IF FromDataSource.PID_VALID = 'Y' THEN FromDataSource.Person_ID ELSE #NULL)
Assuming you are using this code within a Transformer stage in DataStage
this will help
IF Trim(FromDataSource.PID_VALID) = 'Y' THEN Trim(FromDataSource.Person_ID) ELSE #NULL
Hint:
For you next question you might ask in this forum you should provide more details - do not let us guess. Also describe what you have tried and what error you got etc.
You can use case expression :
(CASE WHEN FromDataSource.PID_VALID = 'Y' THEN TRIM(FromDataSource.Person_ID) END)
else will return null if condition evaluate as false, you don't need to specify null.
If you want to set null if PID is not valid then->
IF FromDataSource.PID_VALID = 'Y' THEN trim(FromDataSource.Person_ID) ELSE setnull()
If you want to set empty when PID is not valid then->
IF FromDataSource.PID_VALID = 'Y' THEN trim(FromDataSource.Person_ID) ELSE ''

SQL query using if else

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 ;

CASE statement syntax error in SAP Lumira / Freehand SQL

I’m using SAP Lumira desktop and "Query with SQL (Freehand SQL)", connected to SAP ECC. I try to extend the query with a case statement but run into errors as below:
SELECT "VBUK-UVALS",
CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
WHEN 'B' THEN 'Open'
ELSE 'Other'
END AS "ColumnA"
FROM "Local"."INFOSET"."ZCA_TESTAR"
Syntax error in SQL query:
[line 2:31 missing FROM at 'WHEN'][line 2:36 missing EOF at "A"]
It would be very appreciated if any could guide me through this
As the error suggests, your syntax is wrong. A case expression has only one case keyword, and can have multiple when clauses of the different values you're evaluating (and an optional single else clause):
SELECT "VBUK-UVALS",
CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
WHEN 'B' THEN 'Open' -- CASE ("VBUK-UVALS") removed
ELSE 'Other'
END AS "ColumnA"
FROM "Local"."INFOSET"."ZCA_TESTAR"
Sorry, to many “CASE”, It was a mistake by me when wrote the case.
The Query is stated as followed with the same error message
SELECT "VBUK-UVALS",
CASE ("VBUK-UVALS") WHEN 'A' THEN 'Closed'
WHEN 'B' THEN 'Open'
ELSE 'Other'
END AS "ColumnA"
FROM "Local"."INFOSET"."ZCA_TESTAR"
Syntax error in SQL query:
[line 2:27 missing FROM at 'WHEN'][line 2:32 missing EOF at "A"]

How to use "case-when" in Ecto Queries in elixir?

I have an SQL query like :
SELECT SUM(CASE WHEN <table_name>.status = '2' THEN 1 ELSE 0 END) FROM <table name>.
I want to write the corresponding Ecto Query for the above. Something like:
from t in <table_name>, select: sum(...)
What is the analogy to "case-when" in the above case?
Like the comment said, you can use fragment/1:
query = from t in <Model>, select: fragment("SUM(CASE WHEN status = ? THEN 1 ELSE 0 END)", 2)
If you want to specify the table, this works for me:
query = from t in <Model>, select: fragment("SUM(CASE WHEN ? = ? THEN 1 ELSE 0 END)", t.status, 2)
You can also leverage on macros to extend Ecto query language:
defmacro case_when(condition, do: then_expr, else: else_expr) do
quote do
fragment(
"CASE WHEN ? THEN ? ELSE ? END",
unquote(condition),
unquote(then_expr),
unquote(else_expr)
)
end
end
Then use it like this:
query = from t in <Model>,
select: case_when t.status == 2
do 1
else 0
end

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