Statistics of different values in time span, number of occurrences at that time
, here I want to find out the number of different values in the periods field for each month within 1 years.
For example in the visible 201 May-April, periods 2 appeared 3 times, 3 appeared 1 times, the most critical is periods you do not know how many possible values, perhaps this month 1, perhaps not.
Reply content:
Statistics of different values in time span, number of occurrences at that time
, here I want to find out the number of different values in the periods field for each month within 1 years.
For example in the visible 201 May-April, periods 2 appeared 3 times, 3 appeared 1 times, the most critical is periods you do not know how many possible values, perhaps this month 1, perhaps not.
Wrote a demo, online preview
First, the time format I use is date, 年-月-日
this form
Number of statistics,count再group by 列名
SELECT `periods`,DATE_FORMAT(time,'%Y-%m') as month,COUNT(*) AS `times` FROM `product` GROUP BY `periods`,`month` ORDER BY `month` DESC
DOC:
Date_format:http://www.w3school.com.cn/sql/func_date_format.asp
Group By:http://www.w3school.com.cn/sql/sql_groupby.asp
With the Count function
You can use two group by conditions.
select count(id), periods, from_unixtime(time, '%Y-%m')from productwhere 你的条件group by periods,from_unixtime(time, '%Y-%m')
Well, that's probably the way it looks.
Select Periods,sum (periods) as count from product where month>= ' time ' and mouth<= ' time ' GROUP by periods