sql server中Alias中的月名



是否有办法在别名中有月份名称?

这段代码不工作,但我想要类似的东西。由于

provnam as datename(month,LSTCHG)

select county
,sum(case when svcdat between '2022-01-01' and '2022-01-31' then units end) as jan_units
,sum(case when svcdat between '2022-02-01' and '2022-02-28' then units end) as feb_units
from test
group by county

如果您按月返回分组结果,您可能会找到处理结果的方法:

select 
county, 
datename(month, svcdate) as [Period], 
sum(units) as SumOfUnits
from test
where
svcdate >= '2022-01-01' and svcdate <= '2023-01-01'
group by 
county, datename(month, svcdate)

通过扩展,分组当然也可以包括年份,并且涵盖多个年度。这可以使它更灵活。

相关内容

  • 没有找到相关文章

最新更新