#按月排
SELECT count(EN_NAME), DATE_FORMAT( CREATE_DATE, "%Y-%m" )
FROM finance
WHERE DATE_FORMAT( CREATE_DATE, "%Y" ) = "2016"
GROUP BY DATE_FORMAT( CREATE_DATE, "%Y-%m" )
#按日排
SELECT count(EN_NAME), DATE_FORMAT( CREATE_DATE, "%Y-%m-%d" )
FROM finance
WHERE DATE_FORMAT( CREATE_DATE, "%Y" ) = "2016"
GROUP BY DATE_FORMAT( CREATE_DATE, "%Y-%m-%d" )
这样做的话其实有一点不好,那就是select的维度和group by的里面都会是2018-xx的形式,我想取个月份02都不行,那么现在mysql的时间函数就派上用处了
示例:
select year(m),SUM(vol) as metric_0 from cigarette where DATE_FORMAT(m ,'%Y-%m') = '2018-02' group by year(m) order by metric_0 DESC
同理可得year、month、day函数,更可得hour().啊什么的
但是。。事情往往是不如人意的
比如说,我钻取维度可以只选择年到日,也就是一年点进去可以显示到日。。。
我特么
select DATE_FORMAT(m,"%m-%d") ,SUM(vol) from sales where DATE_FORMAT(m , '%Y') like '2018' group by DATE_FORMAT(m,"%m-%d") order by DATE_FORMAT(m,"%m-%d") ASC
同样的道理,如果从月到日,一开始进来就得是2018-07