I need to run this SQL in my java application to a MySQL database:
update group_table
join
(
select tab1.letter, tab1.id_group_table from
(
select letter,
id_group_table,
count(letter) as occurrences
from letter
group by id_group_table, letter
order by occurrences desc
) tab1
group by tab1.id_group_table having max(tab1.occurrences)
) tab2 on group_table.id_group_table = tab2.id_group_table
set champion = tab2.letter
where group_table.id_whatever in (1,2,3,4);
It works if I try it in the database. Now here's what I'm trying to do using hibernate:
String hqlUpdate = "update group_table join (select tab1.letter, tab1.id_group_table from (select letter,id_group_table, count(letter) as occurrences from letter group by id_group_table, letter order by occurrences desc) tab1 group by tab1.id_group_table having max(tab1.occurrences)) tab2 on group_table.id_group_table = tab2.id_group_table set champion = tab2.letter where group_table.id_whatever in (1,2,3,4);";
getSession().createQuery( hqlUpdate ).executeUpdate();
And here is the error I'm getting:
Ago 04, 2014 12:16:18 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:38: expecting "set", found 'JOIN'
line 1:38: expecting "set", found 'JOIN'
at antlr.Parser.match(Parser.java:211)
at org.hibernate.hql.internal.antlr.HqlBaseParser.setClause(HqlBaseParser.java:414)
I need to run this exact query...
How can I do that with hibernate?
THanks!!
Try following code
String hqlUpdate = "update group_table join (select tab1.letter, tab1.id_group_table from (select letter,id_group_table, count(letter) as occurrences from letter group by id_group_table, letter order by occurrences desc) tab1 group by tab1.id_group_table having max(tab1.occurrences)) tab2 on group_table.id_group_table = tab2.id_group_table set champion = tab2.letter where group_table.id_whatever in (1,2,3,4);";
getSession().createSQLQuery( hqlUpdate ).executeUpdate();
If you need exact query then don't use HQL, use native query support instead.
Related
Iam using coalesce mybatis switch case in my query, where iam getting error like
Error querying database. Cause: java.sql.SQLException: ORA-01427:
single-row subquery returns more than one row
this is my query
(select
(case when (coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null)
then (select sysdate from dual)
else (coalesce(t1.col1,t2.col1, t1.col2, t1.col3))
end )
from table1 t1
join table2 t2
on t1.id IN (t2.id))
Thanks in advance
Seems you have a lot of () but overall you should use = operator and not IN (t2.id) for join t2.id
select
case when coalesce(t1.col1,t2.col1, t1.col2, t1.col3) is null
then sysdate
else coalesce(t1.col1,t2.col1, t1.col2, t1.col3)
end
from table1 t1
join table2 t2 on t1.id = t2.id
And looking at the code you posted in sample you have a select as a column result and this select return several rows, ( this raise the error). You also have a mixin of join syntax some based on explicit join syntax some based on old implicit join syntax based on comma separated table name and where condition. You should try using this
<select id="Trigger" parameterType="hashmap" resultType="java.util.HashMap" flushCache="true">
SELECT
select case when coalesce(table1.col1, table2.col2,table1.col3, table1.col4) is null
then sysdate
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4) end as "ProgressDate"
, table3.id as "ID"
from table1
INNER join table2 on table1.id = table2.id
INNER JOIN table3 ON table1.id = table3.id
INNER JOIN table4 table2.action = table4.action
WHERE table3.transaction = #{inputvaluepassed}
</select>
The query you mention in the question takes the place of a scalar subquery included in another... main query. I formatted the whole query (for readability) and it looks like this:
SELECT
(
select case when coalesce(table1.col1, table2.col2,table1.col3,
table1.col4) is null
then (select sysdate from dual)
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4)
end
from table1
join table2 on table1.id = table2.id
) as "ProgressDate",
table3.id as "ID"
FROM table3, table1, table2, table4
WHERE table3.transaction = #{inputvaluepassed}
AND table1.id = table3.id
AND table2.id=table1.id and table2.action = table4.action
Now, by definition, scalar subqueries can only return zero or one row. In your case it seems that at runtime this subquery is returning multiple rows, and the main query crashes.
You'll need to somehow produce a single row at most: maybe by aggregating the rows (using GROUP BY), maybe by picking one row only from the result set (using LIMIT); there are other options. If we choose the to limit the rows to 1 at most your query could look like:
SELECT
(
select case when coalesce(table1.col1, table2.col2,table1.col3,
table1.col4) is null
then (select sysdate from dual)
else coalesce(table1.col1, table2.col2,table1.col3, table1.col4)
end
from table1
join table2 on table1.id = table2.id
limit 1 -- added this line
) as "ProgressDate",
table3.id as "ID"
FROM table3, table1, table2, table4
WHERE table3.transaction = #{inputvaluepassed}
AND table1.id = table3.id
AND table2.id=table1.id and table2.action = table4.action
This is just one possible cheap solution to the issue. A better understanding on how to pick the right row over multiples ones can produce a better solution.
Following SQL query works perfectly fine in Postgres. It returns all latest trainings of a given exercise.
SELECT th.id, th.date, th.exercise_id
FROM Training th
INNER JOIN (
SELECT exercise_id, MAX(date) AS maxdate
FROM Training
GROUP BY exercise_id
) AS tm ON tm.exercise_id = th.exercise_id AND th.date = tm.maxdate
The problem is that Java JPA fails with
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException unexpected token: (
after the INNER JOIN for the following code example.
String queryString = "SELECT th FROM TrainingHistory th INNER JOIN ( SELECT tm.exercise, MAX(date) as maxdate FROM TrainingHistory group by exercise ) AS tm on (tm.exercise = th.exercise AND th.date = tm.maxdate) WHERE th.accountId = 0";
What am I missing?
i have Three tables [users,projects,scenarios] i need to get latest update projects details based on modified Date column with out duplicate values
the tables are:
users Table :
project table
scenario Table
and i try below query but its return duplicates values if am using group by then old values came but i need latest values
With out Group by query
SELECT
p.`PROJECT_NAME`,
p.`CREATED_DATE`,
s.`MODIFIED_DATE`
FROM
`projects` p
JOIN `scenarios` s ON
s.`PROJECT_ID` = p.`PROJECT_ID`
WHERE
P.`USER_ID` =(
SELECT
USER_ID
FROM
users
WHERE
EMAIL = 'test#gmail.com'
)
ORDER BY
s.`MODIFIED_DATE`
DESC
out put:
with Group by Query :
SELECT
p.`PROJECT_NAME`,
p.`CREATED_DATE`,
s.`MODIFIED_DATE`
FROM
`projects` p
JOIN `scenarios` s ON
s.`PROJECT_ID` = p.`PROJECT_ID`
WHERE
P.`USER_ID` =(
SELECT
USER_ID
FROM
users
WHERE
EMAIL = 'test#gmail.com'
)
group by p.PROJECT_NAME
ORDER BY
s.`MODIFIED_DATE`
DESC
output:
You can separately get the latest scenario per project using a subquery then the resulting rows will then be join again to get the other columns, if needed.
SELECT p.PROJECT_NAME,
p.CREATED_DATE,
s.MODIFIED_DATE
-- all columns in s.* will have the latest row
FROM projects p
INNER JOIN scenarios s
ON p.PROJECT_ID = s.PROJECT_ID
INNER JOIN
(
SELECT project_ID, MAX(modified_date) MAX_modified_date
FROM scenarios
GROUP BY project_ID
) t ON s.project_ID = t.project_ID
AND s.modified_date = t.MAX_modified_date
INNER JOIN users u
ON P.USER_ID = u.USER_ID
WHERE u.EMAIL = 'test#gmail.com'
ORDER BY s.MODIFIED_DATE DESC
However, if you don't need to get the other columns, you can directly use MAX() and GROUP BY.
SELECT p.PROJECT_NAME,
p.CREATED_DATE,
MAX(s.MODIFIED_DATE) AS MAX_MODIFIED_DATE
FROM projects p
INNER JOIN scenarios s
ON p.PROJECT_ID = s.PROJECT_ID
INNER JOIN users u
ON P.USER_ID = u.USER_ID
WHERE u.EMAIL = 'test#gmail.com'
GROUP BY p.PROJECT_NAME,
p.CREATED_DATE
ORDER BY MAX_MODIFIED_DATE DESC
I have the following query. When I execute it I get such error:
[http-nio-8090-exec-9] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42601
[http-nio-8090-exec-9] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: syntax error at or near "."
Position: 5385
I think the problem is in select CG.codes from CodeGroup CG, but how do I write this query correctly? I need to get all the codes that belong to the CodeGroup. The codes is a list of Code.
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("select distinct AI from AppInfo AI ")
.append("left join fetch AI.plan as PSAP ")
.append("where PSAP.edType in ( select C from Code C where C.column1= 'XXXX' ")
.append("and C in (select CG.codes from CodeGroup CG where CG.name = 'YYYY'))");
Your line left join fetch AI.plan as PSAP is wrong. Omit the fetch.
What i have observed that, Your select query has small problem, It should be like this
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("select DISTINCT AI.COLUMN_NAME from AppInfo AI ")
.append("left join fetch AI.plan as PSAP ")
.append("where PSAP.edType in ( select C from Code C where C.column1= 'XXXX' ")
.append("and C in (select CG.codes from CodeGroup CG where CG.name = 'YYYY'))");
Because, AI is the alias name of AppInfo table. Distinct will work on specific column.
Previous related thread: Join in Query WHERE clause
I'm getting this error:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '.'.
My SQL Statement:
SELECT products.id, products.name products.extended_description, products.catalogid, products.image1, products.image2, products.stock, products.price, manufacturer.manufacturer, products.weight
FROM products
JOIN manufacturer ON (products.manufacturer = manufacturer.id)
JOIN product_category ON (product_category.catalogid = products.catalogid)
JOIN category ON (category.id = product_category.id)
WHERE category.category_name = ?;
What do I have wrong here? My statement looks correct to me...
You are missing a comma between products.name and products.extended_description. It thinks that products.extended_description is the alias. If it is then put []s around it. [products.extended_description]. Otherwise put in the missing comma.
SELECT products.id, products.name, products.extended_description, products.catalogid,
products.image1, products.image2, products.stock, products.price,
manufacturer.manufacturer, products.weight
FROM products
JOIN manufacturer ON (products.manufacturer = manufacturer.id)
JOIN product_category ON (product_category.catalogid = product.catalogid)
JOIN category ON (category.id = product_category.id)
WHERE category.category_name = ?;
You forgot a comma after products.name
Should be:
SELECT products.id, products.name, products.extended_description, ...