//公司需要我做一个根据每天用户注册数量生成一个折现图,sql如下,
//亲测好用,只是如果某一天没有注册的话,就不会显示日期
SELECT DATE_FORMAT(f.registDate, '%Y-%m-%d') AS dayRegist,COUNT(f.flowingId) AS dayRegister
FROM shop_invitation_flowerwater AS f
GROUP BY dayRegist
//以 2018-01-22 1:56:55为例 convert(nvarchar(22),CreateDate,10) => 2018-01-22 DATEPART(month,CreateDate) => 01 DATEPART(year,CreateDate) => 2018 //年 select datepart(YEAR,'2018-01-22') select datepart(yyyy,'2018-01-22') select datepart(yy,'2018-01-22') //月 select datepart(MONTH,'2018-01-22') select datepart(mm,'2018-01-22') select datepart(m,'2018-01-22') //日 select datepart(dd,'2018-01-22') //季度 select datepart(qq,'2018-01-22') //星期 select datepart(dw,'2018-01-22') //一年中的第多少天 select datepart(dy,'2018-01-22') //一年中的第多少周 select datepart(wk,'2018-01-22') SELECT CONVERT(VARCHAR(22),GETDATE(),10) --2018-01-22 SELECT CONVERT(VARCHAR(22),GETDATE(),221) --01/22/2018 //按日分组: 2018-01-22 select convert(nvarchar(22),CreateDate,10) as Times,ISNULL(sum(Unit),0.0) as Drinking from pdt_Out group by convert(nvarchar(22),CreateDate,10) //按月分组: 2018-01 select DATEPART(month,CreateDate) as Times,sum(Unit) as Totals from pdt_Out group by DATEPART(month,CreateDate) //按年分组: 2018 select DATEPART(year,CreateDate) as Times,sum(Unit) as Totals from pdt_Out group by DATEPART(year,CreateDate)