SELECT DATE_FORMAT(a.orddate, '%m') 구분 , IFNULL(SUM(CASE 총판 WHEN 'A총판' THEN c.cnt * c.price END), 0) A총판 , IFNULL(SUM(CASE 총판 WHEN 'B총판' THEN c.cnt * c.price END), 0) B총판 , IFNULL(SUM(CASE 총판 WHEN 'C총판' THEN c.cnt * c.price END), 0) C총판 , IFNULL(SUM(CASE 총판 WHEN 'D총판' THEN c.cnt * c.price END), 0) D총판 , SUM(c.cnt * c.price) 합계 FROM order a INNER JOIN basket c ON a.idx = c.ord_idx WHERE a.orddate >= '2023-01-01' AND a.orddate < '2024-01-01' AND 총판 IN ('A총판', 'B총판', 'C총판', 'D총판') GROUP BY DATE_FORMAT(a.orddate, '%m') WITH ROLLUP ;
SELECT 구분 , IFNULL(SUM(CASE 총판 WHEN 'A총판' THEN 금액 END), 0) A총판 , IFNULL(SUM(CASE 총판 WHEN 'B총판' THEN 금액 END), 0) B총판 , IFNULL(SUM(CASE 총판 WHEN 'C총판' THEN 금액 END), 0) C총판 , IFNULL(SUM(CASE 총판 WHEN 'D총판' THEN 금액 END), 0) D총판 , SUM(금액) 합계 FROM (SELECT DATE_FORMAT(a.orddate, '%m') 구분 , 총판 , c.cnt * c.price 금액 FROM order a INNER JOIN basket c ON a.idx = c.ord_idx WHERE a.orddate >= '2023-01-01' AND a.orddate < '2024-01-01' AND 총판 IN ('A총판', 'B총판', 'C총판', 'D총판') UNION ALL SELECT '01', '', 0 UNION ALL SELECT '02', '', 0 UNION ALL SELECT '03', '', 0 UNION ALL SELECT '04', '', 0 UNION ALL SELECT '05', '', 0 UNION ALL SELECT '06', '', 0 UNION ALL SELECT '07', '', 0 UNION ALL SELECT '08', '', 0 UNION ALL SELECT '09', '', 0 UNION ALL SELECT '10', '', 0 UNION ALL SELECT '11', '', 0 UNION ALL SELECT '12', '', 0 ) a GROUP BY 구분 WITH ROLLUP ;