unexpected token : ( subquery hql - sql

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)
)

Related

IN clause with CASE WHEN in SQL

I try to count the number of times a product is ordered two days in a row, over the last seven days.
I tried to implement an IN condition in a CASE WHEN, but I get the following error:
Comparison operator IN not valid
I'm using DB2
Here is what I've tried :
WITH CTE AS (
SELECT ALLPIC, COUNT(DISTINCT(PAL.CODPRO)) AS NBREFSSTOCKS
FROM FGE50NEUV1.GEPAL AS PAL INNER JOIN FGE50NEUV1.GEPIC AS PIC ON PAL.CODPRO = PIC.CODPRO
GROUP BY ALLPIC
),
CTE2 AS (
SELECT ALLSTS AS ALLPIC, COUNT(DISTINCT CASE WHEN DATPRB1 = ` + dateWMS() + ` THEN CODPRO END) AS NBREFSCDE,
COUNT(
DISTINCT CASE WHEN (
CODPRO IN (SELECT DISTINCT(CODPRO) FROM FGE50NEUV1.GESUPD WHERE DATPRB1 = 20221027)
AND CODPRO IN (SELECT DISTINCT(CODPRO) FROM FGE50NEUV1.GESUPD WHERE DATPRB1 = 20221028)
THEN CODPRO END
)
) AS NBCOMMUNVEILLE,
COUNT(
DISTINCT CASE WHEN (
CODPRO IN (SELECT DISTINCT(CODPRO) FROM FGE50NEUV1.GESUPD WHERE DATPRB1 = 20221026)
AND CODPRO IN (SELECT DISTINCT(CODPRO) FROM FGE50NEUV1.GESUPD WHERE DATPRB1 = 20221027)
THEN CODPRO END
)
) AS NBCOMMUNJM2
FROM FGE50NEUV1.GESUPD AS SUP
GROUP BY ALLSTS
)
SELECT * FROM CTE INNER JOIN CTE2 ON CTE.ALLPIC = CTE2.ALLPIC ORDER BY CTE.ALLPIC
Listing of SQL messages:
SQL0115
Message Text:
Comparison operator &1 not valid.
Cause Text:
Simple comparison operators other than equal and not equal cannot be
used with a list of items. ANY, ALL, and SOME comparison operators
must be followed by a fullselect, rather than an expression or a list
of items. Subqueries cannot be specified in a JOIN condition or in a
CASE expression.
Recovery Text:
Change either the comparison or the
operand. Try the request again.

select subquery using data from the select statement?

I have two tables, headers and lines. I need to grab the batch_submission_date from the header table, but sometimes a query for batch_id will return a null for batch_submission_date, but will also return a parent_batch_id, and if we query THAT parent_batch_id as a batch_id, it will then return the correct batch_submission_date.
e.g.
SELECT t1.batch_id,
t1.parent_batch_id,
t2.batch_submission_date
FROM db.headers t1, db.lines t2
WHERE t1.batch_id = '12345';
output = 12345, 99999, null
Then we use that parent batch_id as a batch_id :
SELECT t1.batch_id,
t1.parent_batch_id,
t2.batch_submission_date
FROM db.headers t1, db.lines t2
WHERE t1.batch_id = '99999';
and we get output = 99999,99999,'2018-01-01'
So I'm trying to write a query that will do this for me - anytime a batch_id's batch_submission_date is null, we find that batch_id's parent batch_id and query that instead.
This was my idea - but I just get back null both for bp_batch_submission_date and for new_submission_date.
SELECT
t1.parent_id as parent_id,
t1.BATCH_ID as bp_batch_id,
t2.BATCH_LINE_NUMBER as bp_batch_li,
t1.BATCH_SUBMISSION_DATE as bp_batch_submission_date,
CASE
WHEN t1.BATCH_SUBMISSION_DATE is null
THEN
(SELECT a.BATCH_SUBMISSION_DATE
FROM
db.headers a,
db.lines b
WHERE
a.SD_BATCH_HEADERS_SKEY = b.SD_BATCH_HEADERS_SKEY
and a.parent_batch_id = bp_batch_id
and b.batch_line_number = bp_batch_li
) END as new_submission_date
FROM
db.headers t1,
db.lines t2
WHERE
t1.SD_BATCH_HEADERS_SKEY = t2.SD_BATCH_HEADERS_SKEY
and (t1.BATCH_ID = '12345' or t1.PARENT_BATCH_ID = '12345')
and t2.BATCH_LINE_NUMBER = '1'
GROUP BY
t2.BATCH_CLAIM_LINE_STATUS_DESC,
t1.PARENT_BATCH_ID,
t1.BATCH_ID,
t2.BATCH_LINE_NUMBER,
t1.BATCH_SUBMISSION_DATE;
is what I'm trying to do possible? using the bp_batch_id and bp_batch_li variables
Use CTE (common table expression) to avoid redundant code, then use coalesce() to find parent date in case of null. In your first queries you didn't attach joining condition between two tables, I assumed it's based on sd_batch_headers_skey like in last query.
dbfiddle demo
with t as (
select h.batch_id, h.parent_batch_id, l.batch_submission_date bs_date
from headers h
join lines l on l.sd_batch_headers_skey = h.sd_batch_headers_skey
and l.batch_line_number = '1' )
select batch_id, parent_batch_id,
coalesce(bs_date, (select bs_date from t x where x.batch_id = t.parent_batch_id)) bs_date
from t
where batch_id = 12345;
You could use simpler syntax with connect by and level <= 2 but if in your data there are really rows containing same ids (99999, 99999) then we get cycle error.

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

SQL Error - Column does not exist (in SELECT as)

I am joining two tables: breeds + breed_characteristics (bc)
But I'm getting the following error:
PG::UndefinedColumn: ERROR: column "val" does not exist LINE 11
I'm not sure what's wrong, here is my SQL:
SELECT
breeds.*,
CASE bc.user_val
WHEN NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END AS val
FROM
breed_characteristics bc
INNER JOIN breeds ON breeds.id = bc.breed_id
WHERE bc.characteristic_id = 45
AND val BETWEEN 4 AND 5
ORDER BY val DESC
(Executing this query on Postgres through Active Record)
You can't use expression alias val in where clause like that.
It's because there is an order in which SQL is executed specified in the SQL standard. Here, the WHERE clause is evaluated before SELECT and hence, the WHERE clause is not aware of the alias you created in the SELECT. The ORDER BY comes after the SELECT and hence can utilize aliases.
Just replace the alias with the actual case expression like this:
SELECT
breeds.*,
CASE bc.user_val
WHEN NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END AS val
FROM
breed_characteristics bc
INNER JOIN breeds ON breeds.id = bc.breed_id
WHERE bc.characteristic_id = 45
AND CASE WHEN bc.user_val is NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END BETWEEN 4 AND 5
ORDER BY val DESC
However, you can use alias in order by clause.
One option to avoid restating the CASE expression in multiple places is to use a subquery:
SELECT *
FROM
(
SELECT b.*,
bc.characteristic_id,
CASE WHEN bc.user_val IS NULL THEN bc.value
ELSE (bc.value + (bc.user_val / 2)) / 2
END AS val
FROM breed_characteristics bc
INNER JOIN breeds b
ON breeds.id = bc.breed_id
) t
WHERE t.characteristic_id = 45 AND
t.val BETWEEN 4 AND 5
ORDER BY t.val DESC

With clause subquery availablility for the entire SQL statement in Oracle

I am trying to write a SQL statement which reuses the subquery of the With clause multiple times in Oracle.
With mySubQ as (
...
)
Select Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where mySubQ.Something = SomeTable.Something
This gives me error - ORA-32034 unsupported use of WITH clause
What am I missing?
You need to join with mySubQ, not just define it.
WITH mySubQ AS (...)
SELECT Something
FROM SomeTable
JOIN mySubQ ON mySubQ.Something = SomeTable.Something
WHERE mySubQ.Something >= 0
If you put the query of mySubQ in a subquery, you can't reference mySubQ in the WHERE clause of the main query. Each level of query can only access tables in its own FROM and JOIN clauses, not those of subqueries.
Here is the error: where mySubQ.Something = SomeTable.Something.
The bottom query selects from SomeTable and from the subquery with alias newSubQ,
so mySubQ.Something is not known in this context.
If something is a real column name, not only a "placeholder in the pseudocode", then there is also another error here: Select Something - the column is ambiguous, because both sometable and the subquery have this column.
Try this query:
With mySubQ as (
SELECT * FROM sometable
)
Select newSubQ.Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where newSubQ.Something = SomeTable.Something
;
Demo --> http://www.sqlfiddle.com/#!4/88855/12
This demo contains also another example of using WITH clause:
WITH mySubQ AS (
SELECT *
FROM sometable
),
mySubQ_1 AS (
SELECT *
FROM mySubQ
WHERE somethingelse = 1
),
mySubQ_2 AS (
SELECT *
FROM mySubQ
WHERE something between 2 AND 5
)
SELECT *
FROM sometable s, mySubQ_1 m1,
(
SELECT * FROM mySubQ_2
WHERE something < 10
) m2
WHERE s.something = m1.something
AND m1.somethingelse = m2.somethingelse