sql server 根据日期中的年份分组

根据年进行分组

SELECT DATEPART(YEAR,BuyingTime) from T_Asset group by  DATEPART(YEAR,BuyingTime)

根据年份和月一起分组和排序

SELECT datepart(YEAR,BuyingTime),datepart(MONTH,BuyingTime),COUNT(1),SUM(Price) from T_Asset 
        group by  DATEPART(YEAR,BuyingTime),DATEPART(MONTH,BuyingTime) ORDER BY DATEPART(YEAR,BuyingTime),DATEPART(MONTH,BuyingTime)
原文地址:https://www.cnblogs.com/yinmu/p/14866561.html