sql統計月份
發布時間: 2023-03-27 13:29:58
A. SQL如何按年份月份統計
select left(convert(varchar(10),[datetime],120),7) as 月份,sum(Amount) as 總數
from 表
where [datetime]<='你輸入的時間條件'
group by left(convert(varchar(10),[datetime],120),7)
根據你的數據結果格式重新修改了下:
select cast(datepart(year,[datetime]) as varchar)+'年'+cast(datepart(month,[datetime]) as varchar)+'月' as 月份,sum(Amount) as 總數
from 表
where [datetime]<='你輸入的時間條件' and datepart(year,[datetime])=left('你輸入的時間條件',4)
group by cast(datepart(year,[datetime]) as varchar)+'年'+cast(datepart(month,[datetime]) as varchar)+'月'
熱點內容