使用mysql

时间:2016-05-03 09:21:37

标签: mysql

+--------------+  
| month | year |  
+-------+------+  
| jan   | 2015 |  
| jan   | 2015 |  
| jan   | 2015 |  
| feb   | 2015 |  
| feb   | 2015 |  
| feb   | 2015 |  
| feb   | 2015 |  
| feb   | 2015 |  
| mar   | 2015 |  
| mar   | 2015 |  
| mar   | 2015 |  
| mar   | 2015 |  
| mar   | 2015 |  
| mar   | 2015 |  
| mar   | 2015 |  
| mar   | 2016 |  
| mar   | 2016 |  
| mar   | 2016 |  
| mar   | 2016 |  
| mar   | 2016 |  
| mar   | 2016 |  
| mar   | 2016 |  
| mar   | 2016 |  
| feb   | 2016 |  
| feb   | 2016 |  
| jan   | 2016 |  
| jan   | 2016 |  
| jan   | 2016 |  
| jan   | 2016 |  
+--------------+

这是表格。

我需要每个月的平均率(即月/年)

1 个答案:

答案 0 :(得分:0)

您应该计算month列,而不是*

以下是查询:

SELECT (COUNT(my.`month`)/(SELECT COUNT(*) FROM crate inmn WHERE inmn.`year` = my.`year`)) AS num, my.`month`, my.`year` FROM crate my GROUP BY my.`month`,my.`year;

以上查询的平均值为month,特别是year