ตารางเดิม
rdate ปี-เดือน | infl |
--------------------------------------
2010-1 | 20
2010-2 | 40
2010-3 | 50
----------------------------------------
MYSQL CODE
SELECT
year(r_date) + 543 AS `year`,
SUM(CASE WHEN month(r_date) = '01' THEN format(p.infl, 2) ELSE 0 END) AS jan,
SUM(CASE WHEN month(r_date) = '02' THEN format(p.infl, 2) ELSE 0 END) AS feb,
SUM(CASE WHEN month(r_date) = '03' THEN format(p.infl, 2) ELSE 0 END) AS mar,
SUM(CASE WHEN month(r_date) = '04' THEN format(p.infl, 2) ELSE 0 END) AS apr,
SUM(CASE WHEN month(r_date) = '05' THEN format(p.infl, 2) ELSE 0 END) AS may,
SUM(CASE WHEN month(r_date) = '06' THEN format(p.infl, 2) ELSE 0 END) AS jun,
SUM(CASE WHEN month(r_date) = '07' THEN format(p.infl, 2) ELSE 0 END) AS jul,
SUM(CASE WHEN month(r_date) = '08' THEN format(p.infl, 2) ELSE 0 END) AS aug,
SUM(CASE WHEN month(r_date) = '09' THEN format(p.infl, 2) ELSE 0 END) AS sep,
SUM(CASE WHEN month(r_date) = '10' THEN format(p.infl, 2) ELSE 0 END) AS oct,
SUM(CASE WHEN month(r_date) = '11' THEN format(p.infl, 2) ELSE 0 END) AS nov,
SUM(CASE WHEN month(r_date) = '12' THEN format(p.infl, 2) ELSE 0 END) AS dec,
SUM(CASE WHEN year(r_date) = year(r_date) THEN format(p.infl, 2) ELSE 0 END) AS total
FROM
plantreal24
WHERE
YEAR(r_date) BETWEEN '1974' AND year(CURDATE()) ** ถึงปีปัจจุบันนี้
GROUP BY
year(r_date)
order by YEAR(r_date)
ผลลัพธ์
Year | gjan | gfeb | gmar
--------------------------------------
2010 | 20 | 40 | 50
----------------------------------------