Created
October 26, 2021 09:37
-
-
Save michael-simons/12f0f281c442a73bf7d42332ba7de139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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