Error in query ORA-00979: not a GROUP BY expression - java

I have a query that worked for me when I was using Mysql.
Now I get this error on Oracle :
ORA-00979: not a GROUP BY expression
This is the query:
SELECT o.neNesId, COUNT(o)
FROM ParNe AS o
WHERE o.neBanId = :neBanId
GROUP BY o.neNesId
Any ideas why I have this error?

Your query is:
SELECT o.neNesId, COUNT(o)
FROM ParNe AS o
WHERE o.neBanId = :neBanId
GROUP BY o.neNesId
My guess is that o.o is not a valid field. So, you have a table name where a column name is expected.
Try this instead:
SELECT o.neNesId, COUNT(*)
FROM ParNe AS o
WHERE o.neBanId = :neBanId
GROUP BY o.neNesId
Or replace the * with a valid column name.

use count(*) . count(o) is used in hibernate not in sql query

You need to select count(*) or count(1) as in Gordon's example. If you insist on using your query syntax, which is not necessary at all then try this:
SELECT neNesId, count(o) AS o_cnt
FROM
( -- your query here --
SELECT 1 neNesId, 2 AS o FROM dual
)
GROUP BY neNesId
/

Related

Why org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line? [duplicate]

i have SQL query , i don't know how to write in hibernate
select lp.lnprdtname,la.lid,la.step
from mfic.lnapp as la
left join mfic.lnprdt as lp on lp.lnprdtid in
(select lnprdtid from mfic.lnapp where lid in
(select lid from mfic.lnbrwr where brwrid in
(select brwrid from mfic.brwr where uid=1)))
where la.lid in
(select lid from mfic.lnbrwr where brwrid in
(select brwrid from mfic.brwr where uid=1));
As said here:
Chapter 14. HQL: The Hibernate Query Language - 14.13. Subqueries
from DomesticCat as cat
where cat.name not in (
select name.nickName from Name as name
)
Note that HQL subqueries can occur only in the select or where clauses.
Anyway, I'm not sure it's a good idea to concat so many subqueries in a single statement...

Java - regex on sql query

I need to apply a java regex on sql query string to calculate the count of it. I have to get what is between the "first select" and "from" of the principal query.
This is my example :
Query :
select name,(select age from subtable), adress from table where name in (select name from subtable1)
Result :
select count(*) from table where name in (select name from subtable1)
I was using replaceFirst("^(.*?)from", "select count(*) from") but it is not working because there is an sql query in the attribute.
Please anyone can help ?
You can solve your problem using this regex ^(.*?)from(?![^(]*\\)):
str = str.replaceFirst("^(.*?)from(?![^(]*\\))", "select count(*) from");
Output
select count(*) from table where name in (select name from subtable1)
The idea is match from that is inside () parenthesis.
Demo

ORA-00942: table or view does not exist but query runs on SQL developer

I got an oracle sql query which runs fine on oracle sql developer but when I run it from Java prepared statement sql exception thrown as "ORA-00942: table or view does not exist " and the user got all the privileges for mentioned 3 schemas.
is there any problem with the Query ? currently I can't seems to find the problem and still debugging sql string is also can be executed in SQL developer .
SQL query
SELECT CLI_CLIENT.NAME ,CLI_CLIENT.CLIENT_ID,
AA.SEARCHES,
(SELECT COUNT(*) FROM RES_BOOKING,CLI_WEB_USER WHERE RES_BOOKING.BOOKED_USER=CLI_WEB_USER.ADM_USER_ID
AND TRUNC(RES_BOOKING.BOOKING_DATE) BETWEEN '01-MAR-16' AND '24-MAR-16' AND CLI_WEB_USER.CLIENT_ID=CLI_CLIENT.CLIENT_ID)AS BOOKINGS
FROM CLI_CLIENT,
(SELECT CWU.CLIENT_ID,
COUNT(ST.OPERATION) AS SEARCHES
FROM STAT.ST_TRANSACTION ST , CLI_WEB_USER CWU , CACHE.CACC_CRITERIA CC , CACHE.CACC_CRITERIA_STATS CS
WHERE ST.USER_NAME=CWU.USERNAME
AND ST.OPERATION LIKE 'OTA_HotelAvailRQ%'
AND TRUNC(ST.TRS_TIMESTAMP) BETWEEN '01-MAR-16' AND '24-MAR-16'
AND CWU.CLIENT_ID IN (10975,10040)
AND CC.CRITERIA_ID=CS.CRITERIA_ID
AND CS.SESSION_ID=ST.SESSION_ID
AND CS.DISTRIBUTION_CHANNEL='W'
GROUP BY CLIENT_ID, CWU.CLIENT_ID
)AA
WHERE CLI_CLIENT.CLIENT_ID=AA.CLIENT_ID;
Java Exception
java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
Check if you are connected to the database under the same user account in both SQL Developer and your java application. Most likely this is not the case and your java application has no access to one or more of the tables in the query.
The following is your query in a better view and performance:
select C.NAME,
C.CLIENT_ID,
AA.SEARCHES,
(select count(1)
from RES_BOOKING R
join CLI_WEB_USER U
on U.ADM_USER_ID = R.BOOKED_USER
and trunc(R.BOOKING_DATE)
between '01-MAR-16'
and '24-MAR-16'
and U.CLIENT_ID = C.CLIENT_ID
) AS BOOKINGS
from CLI_CLIENT C
join
(select CWU.CLIENT_ID as CLIENT,
count(ST.OPERATION) AS SEARCHES
from STAT.ST_TRANSACTION ST
join CLI_WEB_USER CWU
on CWU.USERNAME = ST.USER_NAME
join CACHE.CACC_CRITERIA CC
on CC.CRITERIA_ID = CS.CRITERIA_ID
join CACHE.CACC_CRITERIA_STATS CS
on CS.SESSION_ID = ST.SESSION_ID
where ST.OPERATION like 'OTA_HotelAvailRQ%'
and trunc(ST.TRS_TIMESTAMP)
between '01-MAR-16'
and '24-MAR-16'
and CWU.CLIENT_ID in (10975,10040)
and CS.DISTRIBUTION_CHANNEL='W'
group by CLIENT_ID, CLIENT -- OLD OF CLIENT -> CWU.CLIENT_ID
) AA
on AA.CLIENT = C.CLIENT_ID; -- OLD OF AA.CLIENT -> AA.CLIENT_ID
Then divide your query to blocks and execute one by one. For example,
First part:
select C.NAME,
C.CLIENT_ID,
(select count(1)
from RES_BOOKING R
join CLI_WEB_USER U
on U.ADM_USER_ID = R.BOOKED_USER
and trunc(R.BOOKING_DATE)
between '01-MAR-16'
and '24-MAR-16'
and U.CLIENT_ID = C.CLIENT_ID
) AS BOOKINGS
from CLI_CLIENT C;
Second part: take the code inside JOIN block, and so on. If you will get an error in that block, divide more and keep on this.

How to count all groups returned by a group by query in JPQL?

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?

SQL subquery in Hibernate Query Language

I am new to HQL and I am working on subqueries.
I have the following SQL subquery:
select * from (
select * from table order by columnname
) as subquery
where columnvalue = 'somevalue';
I want to fire the query in HQL. I wrote the below code :
Result = session.createQuery("from (from table order by columnname) as subquery where columnvalue = :somevalue")
.setParameter(/*setting all parameters*/)
.list();
I am getting this exception:
QuerySyntaxException : unexpedted token :( line 1, column 10 [from (from ...)]
My SQL query is giving me correct results. How do I write it in HQL ?
I did not think HQL could do subqueries in the from clause
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries
note the sentence:
Note that HQL subqueries can occur only in the select or where clauses.
It will be better if you excute native SQL.

Categories