ARTICLES > SQL

[MySQL] หาค่าเฉลี่ยรายได้ต่อเดือน แบบรายปี Turn Back

2019-06-19 14:55:48

SELECT YR, CONCAT('฿', FORMAT(AVG(PRC), 2)) AVG_MTH, FORMAT(MAX(PRC),2) MX, FORMAT(MIN(PRC),2) MN FROM (

      SELECT CONCAT(month(order_date),'/',year(order_date)) MNT, year(order_date) YR , SUM(net_price) PRC
      FROM `tbl_order` GROUP BY month(order_date), year(order_date) ORDER BY order_date

) a GROUP BY YR ORDER BY a.YR

 

Optional if you want except current month. Add this condition into sub query.

WHERE MONTH(order_date) <> MONTH(CURRENT_DATE())

 

mysql average monthly income by year

 

 

Turn Back