I have error on my Query
1055 Expression #2 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'dms.HrAttLogsFormatted.ScanIn' which is
not functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
Query :
SELECT
Employee.Id as Id,
Employee.FingerId as FingerId,
Employee.Name as Name,
Departement.Departement as Departement,
EmployeeShift.Shift as Shift,
ScanIn
FROM
HrEmployee as Employee
LEFT JOIN HrEmployeeShift as EmployeeShift
ON Employee.ShiftId = EmployeeShift.Id
LEFT JOIN CmDept as Departement
ON Employee.DeptId = Departement.Id
LEFT JOIN (
SELECT
TableEmployee.FingerId,
ScanIn
FROM
HrEmployee as TableEmployee,
HrAttLogsFormatted
WHERE
TableEmployee.FingerId = HrAttLogsFormatted.FingerId
AND DateIn = '2019-11-04'
GROUP BY HrAttLogsFormatted.FingerId
) AS HrJoinLogs
ON Employee.FingerId = HrJoinLogs.FingerId
WHERE
Employee.Status = 1
AND Employee.Flag = 1
AND Employee.ShiftId = 1
AND ScanIn is NULL
GROUP BY
Employee.Name
ORDER BY
Employee.Name ASC
Does anyone have a solution?
First, read about MySQL's notorious non-standard handling of GROUP BY. Here.
Second, it looks like you're using GROUP BY to eliminate duplicates from your result set. That procedure is questionable. In my opinion it's unacceptable when your query deals with other peoples' money. You may want to use SELECT DISTINCT instead. But, your best bet is to figure out the intended logic of this query and rewrite it to deliver that logic more explicitly.
If you still think you need GROUP BY, look at a result set from when your query worked, before you upgraded to a more modern version of MySQL. You will see that all rows of your result set's ScanIn column are NULL, because your query says AND ScanIn IS NULL.
So, grouping by ScanIn won't hurt anything. Add , ScanIn to both GROUP BY clauses.
Related
I was hoping someone could point me in the right direction as to why my data is not ordered by the flight_count column?
SELECT pilot_id,
pilot_firstname,
pilot_lastname,
pilot_email,
licence_num,
flight_count
FROM pilots
INNER JOIN
( SELECT pilot_id,
COUNT(flight_id) AS 'flight_count'
FROM flights
GROUP BY pilot_id
ORDER BY flight_count DESC
) as a
USING (pilot_id);
Move the ordering criteria to the outer select:
SELECT p.pilot_id, p.pilot_firstname, p.pilot_lastname, p.pilot_email, p.licence_num,
fc.flight_count
FROM pilots p
JOIN (
SELECT pilot_id, COUNT(flight_id) AS flight_count
FROM flights
GROUP BY pilot_id
) as fc
on fc.pilot_id = p.pilot_id
ORDER BY fc.flight_count DESC;
Note you should not be 'quoting' column aliases to delimit them, and the name is fine as-is; it's also generally a good idea to use (meaningful) table aliases explicitely, it helps with readability and also means there's less work for the query optimizer to do if columns are explicitely aliased.
Could you help me with this problem please?
SELECT * FROM `message`
WHERE `read_status` =0 AND `reciever` = 'admin-1' GROUP BY `message_thread_code`
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column '3434_decu.message.message_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Your query does not contain any aggregated columns, so GROUP BY has no meaning -- the mysql parser doesn't know what do to with it. (The ONLY_FULL_GROUP_BY error is not exactly the problem.)
So, for example, if you were trying to count the rows that satisfy your condition, you would use:
SELECT COUNT(*) FROM message
WHERE read_status =0 AND reciever = 'admin-1'
GROUP BY message_thread_code
If you simply want to return the filtered list, don't use GROUP BY:
SELECT * FROM message WHERE read_status =0 AND reciever = 'admin-1'
If not, try editing your question to indicate what output you expected to receive.
When I run the following sql, mysql (version 5.7) complains
Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'count_cabinet.cabinet_count' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
The table definition:
CREATE TABLE `bundle_v2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);
CREATE TABLE `cabinet_v2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bundle_id` int(11),
PRIMARY KEY (`id`)
);
The sql:
SELECT bundle_v2.id AS bundle_id,
count_cabinet.cabinet_count AS count_cabinet_cabinet_count
FROM bundle_v2
LEFT OUTER JOIN
(
SELECT cabinet_v2.bundle_id AS bundle_id, count(cabinet_v2.id) AS cabinet_count
FROM cabinet_v2
GROUP BY cabinet_v2.bundle_id
) AS count_cabinet ON count_cabinet.bundle_id = bundle_v2.id
GROUP BY bundle_v2.id
If we set cabinet_v2.bundle_id to be NOT NUL or change LEFT OUTER JOIN to JOIN, everything works fine. But unfortunately we can't do that.
I know the new rule for mysql about group_by, that the columns in the select list should be unique for the value in group_by. But I can't figure out a case when the cabinet_count is not unique with respect to bundle_v2.id. Can someone kindly shed a light on that?
BTW: This sql is part of a big sql, and we have to use subquery here. So removing subquery doesn't work for us.
Fiddler: https://www.db-fiddle.com/f/qJqgLnU6NtPrRBUWQNqr8L/0
The GROUP BY bundle_v2.id in the outer query is meaningless here and it's not an issue of nullable column.
The inner query will return one row for every bundle_id as it is grouping it together and giving you cabinet_v2.id count.
So you can run the query without GROUP BY bundle_v2.id
SELECT bundle_v2.id AS bundle_id,
count_cabinet.cabinet_count AS count_cabinet_cabinet_count
FROM bundle_v2
LEFT OUTER JOIN
(
SELECT cabinet_v2.bundle_id AS bundle_id, count(cabinet_v2.id) AS cabinet_count
FROM cabinet_v2
GROUP BY cabinet_v2.bundle_id
) AS count_cabinet ON count_cabinet.bundle_id = bundle_v2.id
For any reason, if you can't remove the GROUP BY bundle_v2.id from the query, you can use max function on count_cabinet.cabinet_count
SELECT bundle_v2.id AS bundle_id,
MAX(count_cabinet.cabinet_count) AS count_cabinet_cabinet_count
FROM bundle_v2
LEFT OUTER JOIN
(
SELECT cabinet_v2.bundle_id AS bundle_id, count(cabinet_v2.id) AS cabinet_count
FROM cabinet_v2
GROUP BY cabinet_v2.bundle_id
) AS count_cabinet ON count_cabinet.bundle_id = bundle_v2.id
GROUP BY bundle_v2.id
Edit
Query Error: Error: ER_WRONG_FIELD_WITH_GROUP: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'count_cabinet.cabinet_count' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
The error clearly states that you have not used any aggregate function on column count_cabinet.cabinet_count, since it is not in the group by list.
Here you can do a few things depending on your requirement:-
Use a aggregate function on count_cabinet.cabinet_count.
Use it with the group by clause, like this GROUP BY bundle_v2.id, count_cabinet.cabinet_count
You utilize the MySQL extended feature for the group by like this SET sql_mode = ''; and then running your query.
Note: Step 3 can give you unexpected results.
These are not a workaround. These are hard and fast rules of the group by. You need to follow it, or you'll face the error that you have already experienced.
I have couple tables joined in MySQL - one has many others.
And try to select items from one, ordered by min values from another table.
Without grouping in seems to be like this:
Code:
select `catalog_products`.id
, `catalog_products`.alias
, `tmpKits`.`minPrice`
from `catalog_products`
left join `product_kits` on `product_kits`.`product_id` = `catalog_products`.`id`
left join (
SELECT MIN(new_price) AS minPrice, id FROM product_kits GROUP BY id
) AS tmpKits on `tmpKits`.`id` = `product_kits`.`id`
where `category_id` in ('62')
order by product_kits.new_price ASC
Result:
But when I add group by, I get this:
Code:
select `catalog_products`.id
, `catalog_products`.alias
, `tmpKits`.`minPrice`
from `catalog_products`
left join `product_kits` on `product_kits`.`product_id` = `catalog_products`.`id`
left join (
SELECT MIN(new_price) AS minPrice, id FROM product_kits GROUP BY id
) AS tmpKits on `tmpKits`.`id` = `product_kits`.`id`
where `category_id` in ('62')
group by `catalog_products`.`id`
order by product_kits.new_price ASC
Result:
And this is incorrect sorting!
Somehow when I group this results, I get id 280 before 281!
But I need to get:
281|1600.00
280|2340.00
So, grouping breaks existing ordering!
For one, when you apply the GROUP BY to only one column, there is no guarantee that the values in the other columns will be consistently correct. Unfortunately, MySQL allows this type of SELECT/GROUPing to happen other products don't. Two, the syntax of using an ORDER BY in a subquery while allowed in MySQL is not allowed in other database products including SQL Server. You should use a solution that will return the proper result each time it is executed.
So the query will be:
For one, when you apply the GROUP BY to only one column, there is no guarantee that the values in the other columns will be consistently correct. Unfortunately, MySQL allows this type of SELECT/GROUPing to happen other products don't. Two, the syntax of using an ORDER BY in a subquery while allowed in MySQL is not allowed in other database products including SQL Server. You should use a solution that will return the proper result each time it is executed.
So the query will be:
select CP.`id`, CP.`alias`, TK.`minPrice`
from catalog_products CP
left join `product_kits` PK on PK.`product_id` = CP.`id`
left join (
SELECT MIN(`new_price`) AS "minPrice", `id` FROM product_kits GROUP BY `id`
) AS TK on TK.`id` = PK.`id`
where CP.`category_id` IN ('62')
order by PK.`new_price` ASC
group by CP.`id`
The thing is that group by does not recognize order by in MySQL.
Actually, what I was doing is really bad practice.
In this case you should use distinct and by catalog_products.*
In my opinion, group by is really useful when you need group result of agregated functions.
Otherwise you should not use it to get unique values.
I have been struggling with this.I had tried all the approaches like left outer join,group by and even sub queries but couldn't succeed.
Here is my query.
select star_ident,transition_ident,fix_ident,sequence_num,route_type
from corept.std_star_leg where data_supplier='J' and airport_ident='KMMU';
From the above result set i need to extract those rows that has maximum sequence_num for a given transition_ident and star_ident.
Here is my query.
select star_ident,transition_ident,fix_ident,max(sequence_num)
from corept.std_star_leg where data_supplier='J' and airport_ident='KMMU'
group by transition_ident,star_ident;
But the above query is producing wrong results.I even tried joins.
select yt1.star_ident,yt1.transition_ident,yt1.fix_ident
from corept.std_star_leg yt1
left outer join corept.std_star_leg yt2
on (yt1.star_ident = yt2.star_ident and yt1.transition_ident=yt2.transition_ident and yt1.sequence_num < yt2.sequence_num)
where yt1.airport_ident='KMMU' and yt1.data_supplier='J'
and yt2.airport_ident='KMMU' and yt2.data_supplier='J' and yt2.star_ident is null;
But i end up with zero rows.Provide me an efficient way to do this.I need to run this query for 13K entries.Thank you.
You are having nonaggregated columns in your select which are not part of group by.
FROM
MYSQL DOCS
MySQL extends the use of `GROUP BY` so that the select list can refer to nonaggregated
columns not named in the GROUP BY clause. You can use this feature to get better
performance by avoiding unnecessary column sorting and grouping. However, this is
useful primarily when all values in each nonaggregated column not named in the
GROUP BY are the same for each group. The server is free to choose any value from
each group, so unless they are the same, the values chosen are indeterminate.
So to get proper result add all the column of select in group by
EDIT
select b.*
from
corept.std_star_leg b
inner join
(select star_ident,transition_ident,max(sequence_num) as seq
from corept.std_star_leg where data_supplier='J' and airport_ident='KMMU'
group by star_ident,transition_ident) a
on b.star_ident = a.star_ident and a.transition_ident = b.transition_ident and
b.sequence_num = a.seq;
Hope it helps....
Try this:
SELECT *
FROM
(SELECT *
FROM table1
ORDER BY seqno DESC) tmp
GROUP BY `star_ident`,
`trans`
Here is the sqlfiddle
remove group by start_ident
select star_ident,transition_ident,fix_ident,max(sequence_num)
from corept.std_star_leg where data_supplier='J' and airport_ident='KMMU'
group by transition_ident