mysql> SELECT YEAR(whn) AS yr
-> ,SUM(CASE WHEN MONTH(whn) IN (1,2,3) THEN amount END) AS Q1
-> ,SUM(CASE WHEN MONTH(whn) IN (4,5,6) THEN amount END) AS Q2
-> ,SUM(CASE WHEN MONTH(whn) IN (7,8,9) THEN amount END) AS Q3
-> ,SUM(CASE WHEN MONTH(whn) IN (10,11,12) THEN amount END) AS Q4
-> FROM sale
-> GROUP BY YEAR(whn);
+------+------+------+------+------+
| yr | Q1 | Q2 | Q3 | Q4 |
+------+------+------+------+------+
| 2005 | 10 | 40 | 80 | 660 |
| 2006 | 30 | 20 | NULL | NULL |
+------+------+------+------+------+
ถ้าเป็น mysql ใช้
mysql> SELECT QUARTER(whn)
-> ,SUM(CASE WHEN YEAR(whn)=2005 THEN amount END) AS Y2005
-> ,SUM(CASE WHEN YEAR(whn)=2006 THEN amount END) AS Y2006
-> FROM sale
-> GROUP BY QUARTER(whn);