Skip to content

Instantly share code, notes, and snippets.

@michael-simons
Created October 26, 2021 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-simons/12f0f281c442a73bf7d42332ba7de139 to your computer and use it in GitHub Desktop.
Save michael-simons/12f0f281c442a73bf7d42332ba7de139 to your computer and use it in GitHub Desktop.
create table things(id int not null auto_increment, created_at date, user varchar(32), amount decimal(10,2), primary key(id));
insert into things(created_at, user, amount) values (date'2021-10-02', 'michael', 5);
insert into things(created_at, user, amount) values (date'2021-10-02', 'markus', 10);
insert into things(created_at, user, amount) values (date'2021-10-03', 'markus', 15);
insert into things(created_at, user, amount) values (date'2021-10-04', 'markus', 5);
// Correct
select avg(amount) from things;
// Same value but really hard to understand
// https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
select avg(amount), created_at from things;
// Irritating at least (grouping by one column and use another to return)
select avg(amount), user from things group by created_at;
// avg per day
select avg(amount), created_at from things group by created_at;
// avg per day
select avg(amount), user from things group by user;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment