SELECT 'Product_ID', 'Product_Name', 'Total', 'Jan', 'Feb',... //csvのタイトルを出力
UNION
SELECT product_id,
product_name,
Count(*), //合計
Count(CASE
WHEN RIGHT(yyyymm, 2) = '01' THEN 1
END) AS 'Jan', //1月の合計
Count(CASE
WHEN RIGHT(yyyymm, 2) = '02' THEN 1
END) AS 'Feb', //2月の合計
...
...
FROM sales_table
GROUP BY product_id
into outfile 'c:\\prod_monthly.csv' FIELDS TERMINATED BY ',';//csvに出力
0 件のコメント:
コメントを投稿