Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 453
lomaxx Avatar asked Jan 19 '09 00:01

lomaxx


People also ask

Can we use case in HQL?

yes case is working in HQL now (I am using 3.5.

Can we use select * in HQL?

Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like table and column names are case sensitive in HQL.

Is HQL case sensitive?

HQL queries are case insensitive; however, the names of Java classes and properties are case-sensitive HQL is used to execute queries against database.

Is subquery supported in HQL?

Even correlated subqueries (subqueries that refer to an alias in the outer query) are allowed. Note that HQL subqueries can occur only in the select or where clauses. Note that subqueries can also utilize row value constructor syntax.


1 Answers

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

like image 84
Henrik Paul Avatar answered Dec 29 '22 19:12

Henrik Paul