Using a CASE statement in HQL select - nhibernate

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

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

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 ;

SQL Subquery to replace all values

I have a query which returns a bunch of different data, however I want to have it replace all the values upon a certain condition.
What I have written below kind of gives me the result I want but not really. It creates a new column instead of replacing the other one:
SELECT
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
END AS [ShipToCode],
T2.[ShipToCode],
T6.[StreetS],
T6.[StreetNoS],
T6.[CityS],
T6.[ZipCodeS],
T6.[CountryS],
T5.[LicTradNum],
T2.[CardCode],
T4.[Phone1],
T4.[E_Mail],
T4.[U_DestType],
CASE
WHEN T4.[Country] = 'GB'
THEN 'EN'
ELSE T4.[Country]
END AS [Country],
T4.[U_ShortName]
FROM[...]
The end goal is to replace all of the columns with some preset values instead of just ShipToCode as above.
I tried putting an EXIST subquery after FROM too but that didn't work either.
Is this possible? I'm probably missing something very obvious.
Many thanks!
You can use an ELSE in your CASE expression to combine the two "columns":
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
ELSE T2.[ShipToCode]
END AS [ShipToCode],
And by the way, you didn't need to use a Sub-Select. This would work just as well and is easier to read:
CASE
WHEN T4.[U_DestType] = '6' THEN 'Company Limited'
ELSE T2.[ShipToCode]
END AS [ShipToCode],

Oracle: Case in Where clause

How to achieve this in Oracle query. I am getting missing expression error in IN clause
WHERE TBL_DTL_HOST.HOST_METHOD = 'CCtxnPostRq'
AND TRUNC(TBL_DTL_FEATURE.START_DATETIME) BETWEEN TO_DATE (i_startdate, 'DD/MM/YYYY') AND TO_DATE (i_enddate,'DD/MM/YYYY')
AND (SELECTED_DNIS IS NULL OR TBL_DTL_FEATURE.DNIS = SELECTED_DNIS)
AND (TEMP_CUSTOMER_ID IS NULL OR TBL_DTL_FEATURE.CUSTOMER_ID = TEMP_CUSTOMER_ID)
AND
(CASE WHEN i_Feature='All'
THEN (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021'))
ELSE (TBL_DTL_HOST.FEATURE_ID IN ('F020'));
END)
am i missing anything..? Any help would be appreciated..Thanks
Oracle doesn't treat Boolean expressions like other expressions — it handles them as syntax rather than as a type — so CASE expressions can't evaluate to Booleans.
In your case, I think the clearest code is if you just rewrite it a bit:
AND ( TBL_DTL_HOST.FEATURE_ID = 'F020'
OR (i_Feature = 'All' AND TBL_DTL_HOST.FEATURE_ID = 'F021')
)
the last part of the clause should be like this to evaluate correctly
(CASE WHEN i_Feature='All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021')) THEN 1
WHEN i_Feature<>'All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020')) THEN 1
ELSE 0
END) = 1