# Create View
CREATE
ALGORITHM = UNDEFINED
VIEW `member_sum_month`
AS SELECT member_id as id, substring(order_date, 1,7) as dte, sum(total) as total
FROM `view_order` group by member_id, substring( order_date, 1, 7)
Code (SQL)
select
m.id, m.name
, m01.total, m02.total, m03.total
, m04.total, m05.total, m06.total
, m07.total, m08.total, m09.total
, m10.total, m11.total, m12.total
from (
select id , name
, '2014-01' z01, '2014-02' z02, '2014-03' z03
, '2014-04' z04, '2014-05' z05, '2014-06' z06
, '2014-07' z07, '2014-08' z08, '2014-09' z09
, '2014-10' z10, '2014-11' z11, '2014-12' z12
from member
) as m
left join member_sum_month m01 on m01.id=m.id and m01.dte=m.z01
left join member_sum_month m02 on m02.id=m.id and m02.dte=m.z02
left join member_sum_month m03 on m03.id=m.id and m03.dte=m.z03
left join member_sum_month m04 on m04.id=m.id and m04.dte=m.z04
left join member_sum_month m05 on m05.id=m.id and m05.dte=m.z05
left join member_sum_month m06 on m06.id=m.id and m06.dte=m.z06
left join member_sum_month m07 on m07.id=m.id and m07.dte=m.z07
left join member_sum_month m08 on m08.id=m.id and m08.dte=m.z08
left join member_sum_month m09 on m09.id=m.id and m09.dte=m.z09
left join member_sum_month m10 on m10.id=m.id and m10.dte=m.z10
left join member_sum_month m11 on m11.id=m.id and m11.dte=m.z11
left join member_sum_month m12 on m12.id=m.id and m12.dte=m.z12
group by m.id
select
m.id, m.name
, sum(total * (od='2014-01') ) Jan
, sum(total * (od='2014-02') ) Feb
, sum(total * (od='2014-03') ) Mar
, sum(total * (od='2014-04') ) Apr
, sum(total * (od='2014-05') ) May
, sum(total * (od='2014-06') ) Jun
, sum(total * (od='2014-07') ) Jul
, sum(total * (od='2014-08') ) Aug
, sum(total * (od='2014-09') ) Sep
, sum(total * (od='2014-10') ) Oct
, sum(total * (od='2014-11') ) Nov
, sum(total * (od='2014-12') ) `Dec`
from member m
left join (
select member_id as id, substring( order_date , 1, 7) od, sum(total) as total
from view_order group by member_id, substring( order_date , 1, 7)
) as vo on vo.id=m.id group by m.id
select concat(member_fname,' ',member_lname) as member,sum(total) as total , left(order_date,7) as monthreport from view_order
group by member_id,left(order_date,7)