ROLLUP alternative in Hibernate HQL - mysql

I have this query in which I am using Group By with ROLLUP and it works perfectly in MySQL.
SELECT r.Region_Name
, ad.District_Name
, COUNT(me.employee_id)
FROM `master_employee` me
INNER JOIN app_district ad
ON me.`district_id` = ad.`DISTRICT_UID`
INNER JOIN region r
ON ad.REGION_UID = r.REGION_UID
GROUP BY ad.REGION_UID, ad.District_Name WITH ROLLUP
HQL
SELECT new com.eld.chart.ChartValues (r.regionName, dm.districtName, STR(COUNT(me.districtModel)))
FROM MasterEmployeeModel me JOIN me.districtModel dm JOIN dm.regionModel r
GROUP BY ROLLUP(me.districtModel, r.regionName)
shows following exception
17:13:51,311 ERROR [org.hibernate.util.JDBCExceptionReporter] (http-localhost-127.0.0.1-8080-2)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(masterempl0_.district_id, divisionmo2_.REGION_NAME)' at line 1
Is there any valid alternative to ROLLUP for hibernate?

Related

Why does this MySQL error near group by statement?

I want to group my table with mem_num and it appears error saying that :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by m.mem_num' at line 6.
Here's my code.
select m.mem_num, count(d.vid_num) as total
from membership m
inner join rental r on m.mem_num = r.mem_num
inner join detailrental d on r.rent_num = d.rent_num
having count(d.vid_num) > 1
group by m.mem_num;
For a clearer view click here to view the image
:
output query
HAVING belongs after the GROUP BY
select m.mem_num, count(d.vid_num) as total
from membership m
inner join rental r on m.mem_num = r.mem_num
inner join detailrental d on r.rent_num = d.rent_num
group by m.mem_num
having count(d.vid_num) > 1;

MySQL Error: You have an error in your SQL syntax check the manual that corresponds to your MySQL server version for the right syntax to use

I have a SQL query like this:
SELECT E.snum
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (*) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)
But it generates an error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ') >= ALL (SELECT COUNT() FROM enrolled E2 GROUP BY E2.snum)
LIMIT 0, 30' at line 1
What does the error mean? Why it doesn't allow me to use ALL operator?
Try this
SELECT E.snum, COUNT(E.snum)
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (E.snum) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)

SQL : Update table with select

I have a query like this
UPDATE t_prd_cost_compare
SET
2015_AUG_PRD_UNIT_PRICE=i.PRD_UNIT_PRICE,
2015_AUG_PRD_SELLING_PRICE=i.PRD_SELLING_PRICE,
2015_AUG_PRD_IN_PATIENT_LIST_PRICE=i.PRD_IN_PATIENT_LIST_PRICE,
2015_AUG_PRD_OUT_PATIENT_LIST_PRICE=i.PRD_OUT_PATIENT_LIST_PRICE
FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRICE,PRD_OUT_PATIENT_LIST_PRICE
FROM t_product_catalog
LEFT JOIN T_adjust ON IAJ_PRODUCTID=PRODUCTID AND IAJ_ADJNO IS NULL
WHERE PRODUCTID>1 AND (DATE(IAJ_DATE) = '2015-01-01')
GROUP BY IAJ_PRODUCTID
) AS i
WHERE i.PRODUCTID = t_prd_cost_compare.PRODUCTID
I get error like this
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (
SELECT PRODUCTID,PRD_UNIT_PRICE,PRD_SELLING_PRICE,PRD_IN_PATIENT_LIST_PRI' at line 7
I done checked the select statement is correct, but I still get error!
Any idea?
Issue solved, here is the solution
Update
Competition as C
inner join (
select CompetitionId, count(*) as NumberOfTeams
from PicksPoints as p
where UserCompetitionID is not NULL
group by CompetitionID
) as A on C.CompetitionID = A.CompetitionID
set C.NumberOfTeams = A.NumberOfTeams
refer from: mysql update query with sub query

#1064 - You have an error in your SQL syntax: What is wrong with this query?

Please have a look at the below query.
SELECT sub_words.idwords, words_inc.idArticle
(
SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS excl_words, COUNT(sub1.idwords) AS older_words_cnt
FROM words_learned sub0
LEFT OUTER JOIN words_learned sub1
ON sub0.userId = sub1.userId
AND sub0.order < sub1.order
WHERE sub0.userId = 1
GROUP BY sub0.idwords
) sub_words
INNER JOIN words words_inc
ON sub_words.idwords = words_inc.idwords
LEFT OUTER JOIN words words_exc
ON words_inc.idArticle = words_exc.idArticle
AND FIND_IN_SET(words_exc.idwords, sub_words.excl_words)
WHERE words_exc.idwords IS NULL
ORDER BY older_words_cnt
LIMIT 100
This gives the error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS exc' at line 3
I checked the sub query individually and there was no error in the sub query! what is going on here?
You missed the from keyword
SELECT sub_words.idwords, words_inc.idArticle
FROM
( ...

SQL query- Different databases with inner join

I have two DBs- RATINGSAPP and MIGRATIONDATA.
I want to update a table in RATINGSAPP with some values in a table in MIGRATIONDATA. I am trying to run this query:
update r set internal_id = m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
This gives me error:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'from ratingsapp.hotel03 as r inner join migrationdata.migration as m on r.hotel_' at line 1
But a similar select query works for me and gives proper results.
select r.hotel_id, m.internal_id from ratingsapp.hotel03 as r
inner join migrationdata.migration as m on r.hotel_id = m.restaurant_id
What I am doing wrong in the update query?
The correct MySQL syntax is:
update ratingsapp.hotel03 r inner join
migrationdata.migration as m
on r.hotel_id = m.restaurant_id
set internal_id = m.internal_id ;
There is no from clause in a MySQL update. You are using SQL Server/Postgres syntax.