Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'GROUP BY' from SQL query to comply with 'ONLY_FULL_GROUP_BY' #11

Closed
wants to merge 1 commit into from

Conversation

artlab23
Copy link
Contributor

MySQL versions >5.7.5 now have a default 'ONLY_FULL_GROUP_BY' SQL mode, so default MySQL installation will throw a wobbly:
"Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns."
AFAICS, 'time_id' is unique and there is only one 'temperature' set for each row, so I can't see the reason for the "MAX(temperature) as max_c" (which necessitates the 'GROUP BY') ... am I missing something?
I also removed the 'seconds' from the schedules displayed as they take up valuable screen space & tell us nothing.

MySQL versions >5.7.5 now have a default 'ONLY_FULL_GROUP_BY' SQL mode, so default MySQL installation will throw a wobbly:
"Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns."
AFAICS, 'time_id' is unique and there is only one 'temperature' set for each row, so I can't see the reason for the "MAX(temperature) as max_c" (which necessitates the 'GROUP BY') ... am I missing something?
I also removed the 'seconds' from the schedules displayed as they take up valuable screen space & tell us nothing.
@pihome-shc
Copy link
Owner

@artlab23 i think i have to cancel this request, i m working on weekly schedule perhaps that will simply things as well, but without group by time_id and order by start start it goes to all over the shop.

Finaly Query would be:
$query = "SELECT time_id, time_status, DATE_FORMAT(start, '%H:%i') AS formatted_start, DATE_FORMAT(end, '%H:%i') AS formatted_end,start, end, WeekDays, tz_id, tz_status, zone_id, index_id, zone_name, temperature FROM schedule_daily_time_zone_view group by time_id ORDER BY zone_id asc, start asc";

@artlab23
Copy link
Contributor Author

artlab23 commented Jan 7, 2019

Out of interest, which mysql version are you using?

Your query throws the same error for me: "#1055 - Expression #7 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'schedule_daily_time_zone_view.tz_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"

To suppress the error, users can always disable the 'ONLY_FULL_GROUP_BY' mode
- although it seems to revert for some reason (on importing a table or possibly on reboot?), but that's not ideal.

Can you tell me if this query works for you:
SELECT time_id, time_status, DATE_FORMAT(start, '%H:%i') AS formatted_start, DATE_FORMAT(end, '%H:%i') AS formatted_end,start, end, ANY_VALUE(WeekDays), ANY_VALUE(tz_id), ANY_VALUE(tz_status), ANY_VALUE(zone_id), ANY_VALUE(index_id), ANY_VALUE(zone_name), ANY_VALUE(temperature) FROM schedule_daily_time_zone_view group by time_id ORDER BY ANY_VALUE(zone_id) asc, start asc

(It returns no errors on mysql 5.7.24).

Happy new year, btw! ;)

@pihome-shc
Copy link
Owner

@artlab23
i get following Error: SQL Error (1305): FUNCTION pihome.ANY_VALUE does not exist

Server type: MariaDB
Server version: 10.1.23-MariaDB-9+deb9u1 - Raspbian 9.0
Protocol version: 10

@artlab23
Copy link
Contributor Author

Cheers... Ah, mariadb...

So it turns out that this is a known incompatibility between mariadb and mysql , not helped by mariadb not having the 'ANY VALUE' function.

I see now that multiple zones can use the same time_id, hence the need for 'GROUP_BY' (I have only 2 zones... water and heating, so I didn't spot that). I can't really see a way of coding around this issue to keep default installs of mysql and mariadb happy at the same time.

In the mean time, perhaps look at your install guide(s)... there are two - this install guide shows how to install mysql, while this one give instructions on mariadb - unfortunately it's only the first one (mysql) that appears in the 'Recent Articles' sidebar and under the 'Lets Build' tab. The troubleshooting guide indicates that either db can be used.

Maybe just make a note that mysql users need to disable 'ONLY_FULL_GROUP_BY' sqlmode or do the 'ANY VALUE' thing in the query?

I'm up for helping out with the documentation... would a wiki here (on Github) work for you?

@pihome-shc
Copy link
Owner

@artlab23
can you drop me email at info@pihome.eu i will have your account setup on pihome.eu site.
thank you for your offer.

pihome-shc pushed a commit that referenced this pull request Sep 8, 2019
Merge pull request #58 from twa127/master
@pihome-shc
Copy link
Owner

@artlab23
LAMP with MySQL and php5 updated as per instructions:
This pull request no longer required and raspberry pi img file have everything per-installed.

@pihome-shc pihome-shc closed this Feb 8, 2020
@dvdcut
Copy link
Contributor

dvdcut commented Jul 21, 2020

can we re-open this issue. i was trying to setup my pihome on vm and i can not see schedule but when i try to rung query i get following error

SELECT time_id, time_status, `start`, `end`, WeekDays,tz_id, tz_status, zone_id, index_id, zone_name, `category`, temperature, FORMAT(max(temperature),2) as max_c, sch_name 
FROM schedule_daily_time_zone_view WHERE holidays_id = 0 AND tz_status = 1 group by time_id ORDER BY start, sch_name asc;

and google first link is to this issue.

image

@pihome-shc
Copy link
Owner

@dvdcut best to open new issue for this. here is the fix for one query.

SELECT any_value(sdtzv.time_id) as time_id, any_value(sdtzv.time_status) as time_status, any_value(sdtzv.start) as start, any_value(sdtzv.end) as end, any_value(sdtzv.WeekDays) as WeekDays, any_value(sdtzv.tz_id) as tz_id,
any_value(sdtzv.tz_status) as tz_status, any_value(sdtzv.zone_id) as zone_id, any_value(sdtzv.index_id) as index_id, any_value(sdtzv.zone_name) as zone_name, any_value(sdtzv.category) as category, any_value(sdtzv.temperature) as temperature,
FORMAT(max(any_value(sdtzv.temperature)),2) as max_c, any_value(sdtzv.sch_name) as sch_name
FROM schedule_daily_time_zone_view sdtzv
WHERE holidays_id = 0 AND tz_status = 1 group by time_id ORDER BY start, sch_name asc;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants