###前提・実現したいこと
現在、月次売上を集計したものと、予算テーブルを結合しているのですが、うまく結合できません。
また、最終的に集計したものと、予算テーブルを結合して、予算比などを算出したいです
ご教授の程、宜しくお願いします。
SQL
1-- 集計したテーブル 2create table tbl(kaisha int,zokusei int,hiduke date,price int,custom_price int,tax int); 3-- 会社を管理しているテーブル 4create table t_kaisha(kid int unique key,kname varchar(20)); 5-- 販売場所を管理しているテーブル 6create table t_zokusei(zid int unique key,zname varchar(20)); 7-- 目標金額を管理しているテーブル 8create table budget_target(bid int,year int,month int,kid int,zid int,budget int); 9 10insert into t_kaisha values(1,'F社'),(2,'R社'),(3,'C社'),(999,'全社'); 11insert into t_zokusei values(1,'店舗'),(2,'免税'),(3,'国内通販'),(4,'海外通販'),(999,'合計'); 12insert into tbl values 13(1,1,'2017-06-01',100,0,8), 14(1,2,'2017-06-01',0,100,0), 15(1,3,'2017-06-01',100,0,8), 16(1,4,'2017-06-01',0,100,0), 17(2,1,'2017-06-01',100,0,8), 18(2,2,'2017-06-01',0,100,0), 19(2,3,'2017-06-01',100,0,8), 20(2,4,'2017-06-01',0,100,0), 21(3,1,'2017-06-01',100,0,8), 22(3,2,'2017-06-01',0,100,0), 23(3,3,'2017-06-01',100,0,8), 24(3,4,'2017-06-01',0,100,0); 25 26insert into budget_target values 27(1,2017,06,1,1,100), 28(2,2017,06,1,3,100), 29(3,2017,06,1,4,100);
SQL
1select concat(tk.kname,' - ',tz.zname) as kz 2 ,coalesce(kaisha,999) as kaisha 3 ,coalesce(zokusei2,999) as zokusei2 4 ,t1.m6 5from ( 6 select 7 kaisha 8 ,case when zokusei in (1,2) then 1 else zokusei end as zokusei2 9 ,sum((date_format(hiduke,'%m')='06')*(price+custom_price+tax)) as m6 10 from tbl 11 where date_format(hiduke,'%Y')='2017' 12 group by kaisha asc ,zokusei2 asc with rollup 13) as t1 14left join t_kaisha as tk on coalesce(kaisha,999)=tk.kid 15left join t_zokusei as tz on coalesce(zokusei2,999)=tz.zid 16order by kaisha,zokusei2;
###補足情報
MySQL 5.7
回答1件
あなたの回答
tips
プレビュー