I want to lowercase all users usernames, then count how many distinct ones there are, but getting an error on this hql query:
select count(distinct lower(user.username)) from UserEntity as user
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found 'user' near line 1, column 29 [select count(distinct lower(user.username))
This works fine:
select count(distinct user.username) from UserEntity as user
But when I add the lower(...) it it fails, any help is much appreciated!
What about:
select count(lower(user.username)) from UserEntity as user group by lower(user.username)?
My guess is that you are messing with aggregate functions and statements: count(disctinct lower(...)) is a [aggregated function] - [statement] - [aggregated function] pattern which HQL doesn't support (or at least doesn't treat in documentation).
Thanks #Serge Ballesta, I ended up just running this as a SQL query instead of a HQL query, and it worked fine.
Related
while executing the following query using Hibernate
select to_char(vdadCloseDate,'yyyymm'), count(*) from RmDashboardAccountDataBe where 1=1 and vdadRmId in('MK13','MK11') GROUP BY TO_CHAR(vdadCloseDate,'YYYYMM')
I'm getting the following exception,
java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression
Is there any way to handle this issue?
This is "pure" Oracle SQL (i.e. not HQL) which looks exactly like your query (I had to use different table and column names, though):
SQL> select to_char(hire_date, 'yyyymm'), count(*)
2 from employees
3 where department_id in (10, 20)
4 group by to_char(hire_date, 'yyyymm');
TO_CHA COUNT(*)
------ ----------
200309 1
200508 1
200402 1
SQL>
So - yes, it works OK.
This is a link to HQL Group by clause which also suggests that such a query is perfectly valid (have a look so that I wouldn't have to copy/paste its contents over here).
That's why I asked whether you're sure that this is the query that returned ORA-00979 error. As you responded that it is, huh, I wouldn't know what to say ...
SELECT recipe.name,SUM(salesdetails.quantity::integer),recipe.price As Quantity
FROM (salesinfo JOIN salesdetails ON salesinfo.sessionid=salesdetails.salesinfo_sessionid)
JOIN recipe ON salesdetails.recipe_id=recipe.id group by salesdetails.recipe_id,recipe.name,recipe.price
ORDER BY SUM(salesdetails.quantity::integer) DESC;
Can anyone give me the hql query for this?
If you are not acquainted with the HQL and want to use the same query ,then you can do it using the native query feature of the Hibernate like this :
#QUERY(value="SELECT recipe.name, SUM(salesdetails.quantity::
INTEGER),recipe.price AS Quantity
FROM (salesinfo
JOIN salesdetails ON salesinfo.sessionid=salesdetails.salesinfo_sessionid)
JOIN recipe ON salesdetails.recipe_id=recipe.id
GROUP BY salesdetails.recipe_id,recipe.name,recipe.price
ORDER BY SUM(salesdetails.quantity:: INTEGER) DESC", nativeQuery = TRUE)
But I would recommend that you first try to convert the same in into HQL query and then if any issue then you should ask it here instead of directly asking for the converted query. Meanwhile this can help.
If I have a group by query
Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...
How can I count the number of rows not using NativeQuery?
I need to do something like this:
select count(*) from
(Select e.field1, e.field2...
from Entity w
group by e.field1, e.field2...)
Is there any way to do with JPQL?
in oracle you can this hql
Select sum(count(*) - count(*) + 1)
from Entity e
group by e.field1, e.field2...
but dose not work in postgres
Derived tables are not supported in JPQL, so, the best way to do it is to run a native SQL query.
After all, there is a very good reason why both JPA and Hibernate offer support for SQL queries, don't you agree?
Hibernate (HQL) generated the following SQL for which I inserted the parameters:
select
sum(activity0_.calculated_work) as col_0_0_
, employee2_.id as col_1_0_
, projectele1_.id as col_2_0_
from
activity activity0_
inner join generic_object activity0_1_ on activity0_.id=activity0_1_.id
left outer join project_element projectele1_ on activity0_.project_element_id=projectele1_.id
left outer join employee employee2_ on activity0_.owner_id=employee2_.id
left outer join org_unit orgunit3_ on employee2_.org_unit_id=orgunit3_.id
where
activity0_1_.deleted=0 and
activity0_.client_id=22
group by
employee2_.id order by SUM(activity0_.calculated_work) DESC
Error message: Column 'project_element.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I executed this SQL directly in the SQL Server Studio with the same result. I commented this line:
, projectele1_.id as col_2_0_
The SQL was then accepted by the SQL Server
The table project_element definitely has a column with the name id it is also referenced in the LEFT OUTER JOIN and there this column is not causing an error.
Removing the alias projectele1_ had no effect.
To me this looks like a really simple SQL statement. I cannot imagine what is wrong with it and the error message is not helping at all. Any ideas what could be wrong with the SQL?
Your SQL syntax is wrong.If you add projectele1_.id to group by clause it will work.Only aggregate functions work in select statement with group by clause.Or if you remove projectele1_.id from select it will work fine.
My mistake. I should have read the error message several times. projectele1_id is not in the group by clause. MS SQL does not allow to include such a column into the select list. This seems to be a consistency check.
Too bad though that the usage of HQL leads to such an exception in SQL Server but not in MySQL Server.
This is the sentence HQL
select r.response as response from Responsemix as r right join r.idOptQuestion
as opt where opt.idQuestion=5 and opt.content='Other' or opt.content='Others'
order by r.response asc
I don't think the problem with the or in this Hibernate query, apart from a possible logic issue with the order of precedence between OR and AND.
From your query text, we deduce that what you probably wanted is:
... where opt.idQuestion=5 and ( opt.content='Other' or opt.content='Others' )
I believe Hibernate will also allow you to write this as:
... where opt.idQuestion=5 and opt.content in ('Other','Others')
Are you saying that if you remove this portion: "or opt.content='Others'" from the query text, then the query works. But when you add that back in to the query, Hibernate is throwing a java.lang.NullPointerException ? Sweeeeet.