Translate MYSQL query to HQL using multiple JOINS - mysql

everyone.
I am using grails 3.3.0.M2 framework with mysql as data-source the following sql query is working as expected
SELECT
c.name,
SUM(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END) 'open',
SUM(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END) 'in progress',
SUM(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END) 'closed'
FROM
tickets t
INNER JOIN
users u ON t.user_id = u.id
INNER JOIN
user_coordinations uc ON uc.user_id = u.id
INNER JOIN
coordinations c ON c.id = uc.coordination_id
GROUP BY 1
I translated to HQL using implicit JOIN but I am getting the wrong results, here is the hql query:
SELECT
c.name,
SUM(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END)
FROM
Ticket t, User u, UserCoordination uc, Coordination c
WHERE
MONTH(t.dateCreated) = :month
GROUP BY 1
In order to get the right results stack overflow users help me to understand that the query needs to use explicit JOINS, here the question: Group by a field that does not belongs to the consulted table
Right now I am trying with the following query:
SELECT
c.name,
SUM(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END),
SUM(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END)
FROM
Ticket t
INNER JOIN
User u
INNER JOIN
UserCoordination uc
INNER JOIN
Coordination c
WHERE
MONTH(t.dateCreated) = :month
GROUP BY 1
But i am getting a com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException with the caused message 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 'inner join user_coordinations usercoordi2_ on inner join coordinations coordinat' at line 1
Thanks for your help and time

SELECT new map(
c.name as name,
(CASE
WHEN t.status = 'open' THEN 1
ELSE 0
END) as open,
(CASE
WHEN t.status = 'pending' THEN 1
ELSE 0
END) as pending,
(CASE
WHEN t.status = 'closed' THEN 1
ELSE 0
END) as closed,
SUM(open) as openSum,
SUM(pending) as pendingSum,
SUM(closed) as closedSum
)
FROM
Ticket t
left join t.user u left join u.userCoordination uc left join uc.coordination c
WHERE
MONTH(t.dateCreated) = :month
//GROUP BY 1
What you had had lots missing above is more like what you need, you need
select new map(i.item as item... if you compare the basics of this with what you had and what i tried to do you can see why you had errors.
unsure about your group by it should be group by something. Wasn't sure by inner join if you just meant a join if that was the case leave out all the left join since left join attempts to connect and get any null hasMany relations etc.

Related

MS SQL case inside count

I have zero experience in MS SQL and I'm tearing my hair out trying to convert my MySQL query:
select
m.Customer,
count(if(c.AtFault="Me",1,null)) 'MeLate',
count(if(c.AtFault="You",1,null)) 'YouLate',
count(*) 'Total'
from m
left join r on m.OrderNumber = r.OrderNumber
left join c on r.ReasonCodeID = c.ID
group by m.Customer
This is what I'm trying to run on MS SQL:
select
m.Customer
count(CASE WHEN c.AtFault="Me" THEN 1 ELSE null) 'MeLate',
count(CASE WHEN c.AtFault="You" THEN 1 ELSE null) 'YouLate',
count(*) 'Total'
from m
left join r on m.OrderNumber = r.OrderNumber
left join c on r.ReasonCodeID = c.ID
group by m.Customer
but this throws the uninformative error:
SQL Error (102): Incorrect syntax near ')'.
Three errors:
You are missing the END at the end of CASE WHEN.
You must use single quote instead double.
Forgot the comma after m.Customer
Try this:
select
m.Customer,
count(CASE WHEN c.AtFault='Me' THEN 1 ELSE null END) MeLate,
count(CASE WHEN c.AtFault='You' THEN 1 ELSE null END) YouLate,
count(*) Total
from m
left join r on m.OrderNumber = r.OrderNumber
left join c on r.ReasonCodeID = c.ID
group by m.Customer
end is missing there and you can try sum instead of count
select
m.Customer,
sum(CASE WHEN c.AtFault='Me' THEN 1 ELSE 0 end) MeLate,
sum(CASE WHEN c.AtFault='You' THEN 1 ELSE 0 end) YouLate,
count(*) Total
from m
left join r on m.OrderNumber = r.OrderNumber
left join c on r.ReasonCodeID = c.ID
group by m.Customer

MYSQL - cant create view

SELECT branches.brid,
COALESCE(a.cnt, 0) AS Assigned,
COALESCE(c.cnt, 0) AS Completed,
COALESCE(p.cnt, 0) AS Pending,
COALESCE(x.cnt, 0) AS Expired
FROM branches WHERE access = 'User'
LEFT JOIN
(SELECT brid, count(*) from task GROUP BY brid) a ON branches.brid = a.brid
LEFT JOIN
(SELECT brid, count(*) from task WHERE stat = 'Completed' GROUP BY brid) c ON branches.brid = c.brid
LEFT JOIN
(SELECT brid, count(*) from task WHERE stat = 'Pending' GROUP BY brid) p ON branches.brid = p.brid
LEFT JOIN
(SELECT brid, count(*) from task WHERE stat = 'Expired' GROUP BY brid) x ON branches.brid = x.brid
ORDER BY branches.brid ASC;
I got this error when i create view is there any way to do this? this query works but i can't create view in my data base
ERROR 1349 (HY000): View's SELECT contains a subquery in the FROM clause
The error more or less explains itself.
Here is an excerpt from the documentation, you need to run MySQL 5.7.7 or newer to be able to use a subquery in a view.
view definition is subject to the following restrictions:
Before MySQL 5.7.7, the SELECT statement cannot contain a subquery
in the FROM clause.
....
You can probably rewrite your query to something like this:
SELECT branches.brid,
COUNT(t.stat) AS Assigned,
SUM(CASE WHEN t.stat = 'Completed' THEN 1 ELSE 0 END) AS Completed,
SUM(CASE WHEN t.stat = 'Pending' THEN 1 ELSE 0 END) AS Pending,
SUM(CASE WHEN t.stat = 'Expired' THEN 1 ELSE 0 END) AS Expired
FROM branches
LEFT JOIN task t ON branches.brid = t.brid
WHERE access = 'User'
GROUP BY branches.brid
ORDER BY branches.brid ASC;

refrence parent field from sub inner join select

I have this query that select records from a table and LEFT JOINS other two tables.
Here's the query
SELECT mainTest.lab_number_auto, mainTest.lab_number,
SUM(CASE WHEN test.lab_number = mainTest.lab_number
THEN test.test_unit_cost ELSE 0 END) cost,
payment.totalPaid
FROM patient_main_test mainTest
LEFT JOIN patient_test test
ON test.lab_number = mainTest.lab_number
LEFT JOIN (
SELECT SUM(CASE WHEN testpayment.lab_number = mainTest.lab_number THEN testpayment.amount_paid ELSE 0 END) totalPaid
FROM patient_test_payment testpayment
GROUP BY testpayment.lab_number
) AS payment
ON payment.lab_number = mainTest.lab_number
WHERE mainTest.lab_number != ''
GROUP BY mainTest.lab_number
On running this query i get this error
MySQL said:
#1054 - Unknown column 'mainTest.lab_number' in 'field list'
Thanks.
Here's a fiddle i created.
http://sqlfiddle.com/#!2/291c4/8
Try this, if im right the issue is that you were trying to treat your derived table in the left join like a co-related sub-query. & thx for the sql fiddle... makes it so much easier to help you.
SELECT mainTest.lab_number_auto, mainTest.lab_number,
SUM(CASE WHEN test.lab_number = mainTest.lab_number
THEN test.test_unit_cost ELSE 0 END) cost,
(
SELECT SUM(CASE WHEN testpayment.lab_number = mainTest.lab_number THEN testpayment.amount_paid ELSE 0 END) totalPaid
FROM patient_test_payment testpayment
Where lab_number = mainTest.lab_number
GROUP BY testpayment.lab_number
) AS totalPaid
FROM patient_main_test mainTest
LEFT JOIN patient_test test
ON test.lab_number = mainTest.lab_number
WHERE mainTest.lab_number != ''
GROUP BY mainTest.lab_number

MySQL: using Case When Then End for multiple Inner Join tables

I have a query which returns results using 'Case When Then End' from three tables which are joind. It looks like this:
select f.filename,
sum(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser,
sum(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,
sum(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather
from VMdata v
inner join files f on v.id = f.id
inner join DMdata d on f.id = d.id
where f.filename in (X,Y,Z)
group by f.filename
This works fine, with each resulting-row giving eaither 1 or 0 correctly. The thing to note here is that each table has only one entry(row) for a specific "filename".
Now when I try to add another Inner Join with a table which can have multiple entries(rows) per "filename", the result becomes wrong in a way that only the last 'sum' shows the correct values whereas other 'sums' give wrong values. This second query is:
select f.filename,
sum(case when v.rUser like '%bike%' then 1 else 0 end) as bikeUser,
sum(case when v.rUser like '%Pedestrian%' then 1 else 0 end) as pedestrianUser,
sum(case when d.weather like '%clear%' then 1 else 0 end) as clearWeather,
sum(case when m.extras like '%hat%' then 1 else 0 end) as hatExtras
from VMdata v
inner join files f on v.id = f.id
inner join DMdata d on f.id = d.id
inner join MultiFiledata m on f.id = m.id
where f.filename in (X,Y,Z)
group by f.filename
Any idea to get the right figures for all columns?
If the MultiFiledata table can contain multiple records for any corresponding record in the files table, then you'll need to do the aggregate in a separate query and join that back to the main query.
For example (syntax may not be perfect; concentrate on concept):
select f.filename,
sum(case when v.rUser like '%bike%'
then 1 else 0 end) as bikeUser,
sum(case when v.rUser like '%Pedestrian%'
then 1 else 0 end) as pedestrianUser,
sum(case when d.weather like '%clear%'
then 1 else 0 end) as clearWeather
from VMdata v
inner join files f
on v.id = f.id
inner join DMdata d
on f.id = d.id
inner join (
select id,
sum(case when extras like '%hat%'
then 1 else 0 end) as hatExtras
from MultiFiledata
) m
on f.id = m.id
where f.filename in (X,Y,Z)
group by f.filename;
Your one-to-many join is causing the overall row count to increase, such that when your value=1, you are adding that value more than once. To mitigate this, you may want to use the count function instead, using it to count distinct user IDs. Something like:
Count(distinct case when [logic goes here] then [user ID] else null end) as bikeuser
Then, you are only counting each user once rather than adding each row.

Mysql COUNT result rows for a related table

I have
users
------------------------
id | name | other_stuff.....
.
engagement
------------------------
user_id | type_code |
type_code is a varchar, but either A, B, C or NULL
[ EDIT for clarity: Users can have many engagements of each type code. SO I want to count how many they have of each. ]
I want to return ALL user rows, but with a count of A, B and C type engagements. E.g.
users_result
------------------------
user_id | user_name | other_stuff..... | count_A | count_B | count_C |
I've done quite a bit of searching, but found the following issues with other solutions:
The "other_stuff..." is actually grouped / concatenated results from a dozen other joins, so it's a bit of a monster already. So I need to be able to just add the additional fields to the pre-existing "SELECT ...... FROM users..." query.
The three additional required bits of data all come from the same engagement table, each with their own condition. I havent found anything to allow me to use the three conditions on the same related table.
Thanks
[edit]
I tried to simplify the question so people didn't have to look through loads of unnecessary stuff, but seems I might not have given enough info. Here is 'most' of the original query. I've taken out a lot of the selected fields as there are loads, but I've left most of the joins in so you can see basically what is actually going on.
SELECT
user.id,
user.first_name,
user.second_name,
GROUP_CONCAT(DISTINCT illness.id ORDER BY illness.id SEPARATOR ',' ) AS reason_for_treatment,
IF(ww_id=1000003, 1,'') as user_refused_program,
Group_CONCAT(DISTINCT physical_activity.name SEPARATOR ', ') AS programme_options,
COUNT(CASE WHEN engagement_item.type_code LIKE 'wm6%' THEN 1 ELSE NULL END) as count_A,
COUNT(CASE WHEN engagement_item.type_code LIKE 'wm12%' THEN 1 ELSE NULL END) as count_B,
COUNT(CASE WHEN engagement_item.type_code LIKE 'wm6%' THEN 1 ELSE NULL END) as count_C
FROM `user`
LEFT JOIN session AS session_induction ON (user.id = session_induction.user_id AND session_induction.session_type_id = 3)
LEFT JOIN stats AS stats_induction ON session_induction.id = stats_induction.session_id
LEFT JOIN session AS session_interim ON (user.id = session_interim.user_id AND session_interim.session_type_id = 4)
LEFT JOIN stats AS stats_interim ON session_interim.id = stats_interim.session_id
LEFT JOIN session AS session_final ON (user.id = session_final.user_id AND session_final.session_type_id = 5)
LEFT JOIN stats AS stats_final ON session_final.id = stats_final.session_id
LEFT JOIN user_has_illness ON user.ID = user_has_illness.user_id
LEFT JOIN illness ON user_has_illness.illness_id = illness.id
LEFT JOIN user_has_physical_activity ON user.ID = user_has_physical_activity.user_id
LEFT JOIN physical_activity ON user_has_physical_activity.physical_activity_id = physical_activity.id
LEFT JOIN engagement_item ON user.ID = engagement_item.user_ID
WHERE (user.INDUCTION_DATE>='2010-06-09' AND user.INDUCTION_DATE<='2011-06-09' AND user.archive!='1' )
GROUP BY user.id, engagement_item.user_id
It's worth mentioning that it works fine - returns all users with all details required. Except for the count_A B and C cols.
[edit added slightly more simplified query below]
Stripped out the unrelated joins and selects.
SELECT
user.id,
user.first_name,
COUNT(CASE WHEN engagement_item.type_code LIKE 'wm6%' THEN 1 ELSE NULL END) as count_A,
COUNT(CASE WHEN engagement_item.type_code LIKE 'wm12%' THEN 1 ELSE NULL END) as count_B,
COUNT(CASE WHEN engagement_item.type_code LIKE 'wm6%' THEN 1 ELSE NULL END) as count_C
FROM `user`
LEFT JOIN engagement_item ON user.ID = engagement_item.user_ID
GROUP BY user.id, engagement_item.user_id
SELECT e.user_id, u.name,
COUNT(CASE type_code WHEN 'A' THEN 1 ELSE NULL END) as count_A,
COUNT(CASE type_code WHEN 'B' THEN 1 ELSE NULL END) as count_B,
COUNT(CASE type_code WHEN 'C' THEN 1 ELSE NULL END) as count_C
FROM engagement e join users u on (e.user_id = u.id)
GROUP BY e.user_id, u.name
I would use COUNT instead of SUM just because that is what it is made for, counting things when not NULL.
SELECT
user.id,
user.first_name,
user.second_name,
GROUP_CONCAT(DISTINCT illness.id ORDER BY illness.id SEPARATOR ',' ) AS reason_for_treatment,
IF(ww_id=1000003, 1,'') as user_refused_program,
Group_CONCAT(DISTINCT physical_activity.name SEPARATOR ', ') AS programme_options,
ei.count_A, ei.count_B, ei.count_C
FROM `user`
LEFT JOIN ( SELECT user_id
, COUNT(CASE WHEN engagement_item.type_code LIKE 'wm6%' THEN 1 ELSE NULL END) as count_A
, COUNT(CASE WHEN engagement_item.type_code LIKE 'wm12%' THEN 1 ELSE NULL END) as count_B
, COUNT(CASE WHEN engagement_item.type_code LIKE 'wm6%' THEN 1 ELSE NULL END) as count_C
FROM engagement_item
GROUP BY userid ) ei
LEFT JOIN session AS session_induction ON (user.id = session_induction.user_id AND session_induction.session_type_id = 3)
LEFT JOIN stats AS stats_induction ON session_induction.id = stats_induction.session_id
LEFT JOIN session AS session_interim ON (user.id = session_interim.user_id AND session_interim.session_type_id = 4)
LEFT JOIN stats AS stats_interim ON session_interim.id = stats_interim.session_id
LEFT JOIN session AS session_final ON (user.id = session_final.user_id AND session_final.session_type_id = 5)
LEFT JOIN stats AS stats_final ON session_final.id = stats_final.session_id
LEFT JOIN user_has_illness ON user.ID = user_has_illness.user_id
LEFT JOIN illness ON user_has_illness.illness_id = illness.id
LEFT JOIN user_has_physical_activity ON user.ID = user_has_physical_activity.user_id
LEFT JOIN physical_activity ON user_has_physical_activity.physical_activity_id = physical_activity.id
LEFT JOIN engagement_item ON user.ID = engagement_item.user_ID
WHERE (user.INDUCTION_DATE>='2010-06-09' AND user.INDUCTION_DATE<='2011-06-09' AND user.archive!='1' )
GROUP BY user.id, engagement_item.user_id, ei.count_A, ei.count_B, ei.count_C
Something like this perhaps?
select e.user_id, u.name,
sum(case e.type_code when 'A' then 1 else 0 end) as count_A,
sum(case e.type_code when 'B' then 1 else 0 end) as count_B,
sum(case e.type_code when 'C' then 1 else 0 end) as count_C
from engagement e join users u on (e.user_id = u.id)
group by e.user_id, u.name
The interesting part is the use of CASE inside the SUM to split the counting into three chunks.