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)

 

posted @ 2021-06-09 13:44  饮木  阅读(681)  评论(0编辑  收藏  举报