HQL syntax problem - sql

I have a problem with the following HQL query:
select sum(MYTABLE.COUNTER) from (
select count(DISTINCT bi.products.id) as COUNTER
from BusinessInformation as bi
where bi.informationOwners.id in (100)
and bi.products.id in (10)
and bi.valueAmount not in ('NA')
and ((bi.valueType = 'ACHIEVED' and bi.referenceYears.id = 1) or (bi.valueType = 'FINAL_BALANCE' and bi.referenceYears.id = 2))
group by bi.informationOwners.id
) MYTABLE
The compiler reports:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 34
Do you have any idea what's wrong with the query? I tested the inner query and it works fine.
Thanks,
C

HQL subqueries can occur only in the select or where clauses, not FROM.

Related

sql server error msg 116, how to bypass

I have a problem with a subquery in my query.
In this query :
SELECT *
FROM Statistic_RecordedConversations A
JOIN (SELECT DID, max(DateTime) MaDate
FROM Statistic_RecordedConversations
where DID IN (
Select OpenData as DID, QuestionID, InterviewID
from Surveys.dbo.Askia2363Data
where QuestionID = 895
and InterviewID in (
select Surveys.dbo.Askia2363Data.InterviewID
from Surveys.dbo.Askia2363Interview,
Surveys.dbo.Askia2363Data
where Surveys.dbo.askia2363Interview.InterviewID =
Surveys.dbo.Askia2363Data.InterviewID
and completed = 1
and QuestionID = 891
and ClosedData = 4685
and CAST(EndInterview as Date) =
CAST (Current_TIMESTAMP as Date)))
GROUP BY DID ) B
ON A.DID = B.DID
AND A.DateTime = B.MaDate
I got an error with msg 116. I think its due to the ligne 6 where I have not only 1 column. I don't know how to get this query working. I tried EXISTS insteand of IN but got too many results and not the results that are interesting to me.
If someone got another solution, many thanks! :)
You can only return one field to correlate inside of an IN statement. You need to only return DID.
where DID IN ( Select OpenData as DID from Surveys.dbo.Askia2363Data...

How to write sub query with where Condition and match the columns values

the following is my query
select * from tbl_incometax_master where (select Slabtitle=Gender,SlabSubTitle=Senior_CTZN_Type FROM etds.dbo.tbl_Employee_Master WHERE employee_id = 1218 AND company_id = 1987)
when try to execute got following error in sql server 2008 r2 :
Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Perhaps this is what you intend to do (Uses EXISTS):
select *
from tbl_incometax_master
where exists (
select 1
from etds.dbo.tbl_Employee_Master
where employee_id = 1218
and company_id = 1987
and Slabtitle = Gender
and SlabSubTitle = Senior_CTZN_Type
)
It's a correlated subquery, where the outer query checks if there is at least one row exists in the subquery for current row.
You dont have any column in where clause of parent query, it is important to know that only column criteria can be defined with subquery.
Sample query...
select * from tbl_incometax_master where <column> (select <subquery column> FROM etds.dbo.tbl_Employee_Master WHERE employee_id = 1218 AND company_id = 1987)
You have a select clause in your Where with no operator (=,<,>,IN) ! And '=' inside the Select!
None of them are allowed in a SQL Query.
select * from tbl_incometax_master table1 LEFT JOIN
(
select Gender,Senior_CTZN_Type FROM etds.dbo.tbl_Employee_Master WHERE employee_id = 1218 AND company_id = 1987
) table2
ON
table1.Slabtitle=table2.Gender
AND table1.SlabSubTitle=table2.Senior_CTZN_Type
WHERE table2.Slabtitle IS NOT NULL

simple subquery not working DB2

hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)

unexpected token : ( subquery hql

Here's my HQL query
FROM com.mysite.ActeurInterne act WHERE act.acteurId IN
(SELECT DISTINCT COALESCE(acteurInterne.acteurInternePrincipalId, acteurInterne.acteurId)
FROM
(SELECT DISTINCT acteurInterne
FROM com.mysite.ActeurInterne AS acteurInterne
JOIN acteurInterne.roleSet.roles AS role
WHERE acteurInterne.acteurId = acteurInterne.acteurId
AND acteurInterne.nom LIKE :likenom
AND (role.dateFermeture IS NULL
OR role.dateFermeture >= TRUNC(SYSDATE))
AND (role.dateOuverture IS NULL
OR role.dateOuverture <= TRUNC(SYSDATE))
AND (role.type = :type
OR role.type = :typeC)
)
)
I get
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 190
which is the "(" at the begining of the fourth line above.
( SELECT DISTINCT acteurInterne
Hibernate documentation states that sub-queries are allowed only in the SELECT or WHERE clause.
Note that HQL subqueries can occur only in the select or where
clauses.
But in the example above you have a subquery in the FROM clause of the first subquery.
Have you tried consolidating the 2 sub-queries into one?
FROM com.mysite.ActeurInterne act WHERE act.acteurId IN
(SELECT DISTINCT COALESCE(acteurInterne.acteurInternePrincipalId, acteurInterne.acteurId)
FROM com.mysite.ActeurInterne AS acteurInterne
JOIN acteurInterne.roleSet.roles AS role
WHERE acteurInterne.acteurId = acteurInterne.acteurId
AND acteurInterne.nom LIKE :likenom
AND (role.dateFermeture IS NULL
OR role.dateFermeture >= TRUNC(SYSDATE))
AND (role.dateOuverture IS NULL
OR role.dateOuverture <= TRUNC(SYSDATE))
AND (role.type = :type
OR role.type = :typeC)
)

Why does this SQL query that works in MS Access not work in SQL Server?

The following query works in MS Access, but it does not work in MS SQL Server:
SELECT
tblSession.PatientID as PID,
max(tblSession.SessionAttend) -
min(tblSession.SessionAttend) + 1 as NumSA,
max(tblSession.SessionSched) -
min(tblSession.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
tblSession.SessionNumber,
tblSession.SessionDate,
tblSession.SessionAttend,
tblSession.SessionSched FROM
tblPatient INNER JOIN tblSession ON
tblPatient.PatientID = tblSession.PatientID) WHERE
(tblSession.SessionDate >= '12/8/2010') AND
(tblSession.SessionDate <= '5/18/2011') AND
(tblSession.Status = '2') ORDER BY
tblSession.PatientID, tblSession.SessionNumber
) GROUP BY tblSession.PatientID
In SQL Server, it gives the error "Incorrect syntax near the keyword 'GROUP'." When I hover over the GROUP keyword, the tooltip displays "Incorrect syntax near 'GROUP'. Expecting AS, ID, or QUOTED_ID." I don't understand. Can anyone tell me how to make this query work?
The derived table in () needs an alias, and its column references in the SELECT list updated accordingly:
SELECT top 100 percent
ALIASNAME.PatientID as PID,
max(ALIASNAME.SessionAttend) -
min(ALIASNAME.SessionAttend) + 1 as NumSA,
max(ALIASNAME.SessionSched) -
min(ALIASNAME.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
...
...
...
...
tblSession.PatientID, tblSession.SessionNumber
-- This derived table needs an alias
) ALIASNAME
GROUP BY ALIASNAME.PatientID