そうですね
集計したテーブルをサブクエリにして
カレンダーテーブルにleft joinしてあげてください
追記
ちょっとわかりにくいかもしれないのでプロシージャの書き方を追記しておきます。
SQL
1create table sales(date date,price int);
2insert into sales values
3('2017-05-01',12000),
4('2017-05-02',12000),
5('2017-05-02',8000),
6('2017-05-02',8000),
7('2017-05-05',15000),
8('2017-05-05',6000);
SQL
1drop procedure if exists temp_cal;
2delimiter //
3create procedure temp_cal(in min date ,in max date)
4begin
5create temporary table temp(date date);
6set @d=min;
7while @d<=max do
8insert into temp values(@d);
9set @d:=@d + interval 1 day;
10end while;
11end
12//
13delimiter ;
14
- プロシージャをcallしつつ、カレンダーに連結します
SQL
1call temp_cal('2017-05-01','2017-05-05');
2select t1.date,coalesce(sum_price,0) as sum_price from temp as t1
3left join (select date,sum(price) as sum_price from sales group by date) as t2
4on t1.date=t2.date
5order by date;
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/24 06:58