这种情况如何对月份分组后取值?
有一个表VOYAGE,里面有两个字段ATA_DATE,TEU_COUNT
请问我传入一个时间段V_F_ATA,V_T_ATA
怎么取到表中我要的时间段的TEU_COUNT值呢?
主要的问题是要按月份分组,其中没有数据的月份要显示出来且显示为0
比如我传入的时间段是 2006-1-1~2006-12-31 有可能8月没有数据,但也要显示0
------解决方案--------------------对,手误
select top 12 id=identity( int,1,1) into # from syscolumns a,syscolumns b
select convert(varchar(7),ata_date,120) 月份,sum(TEU_COUNT) TEU_COUNT
from VOYAGE a,# b
where datediff(month,dateadd(month,b.id,V_F_ATA),V_T_ATA)> =0
group by convert(varchar(7),ata_date,120) 月份
------解决方案--------------------select top 365 intDate=identity(int,38716,1) into #intDate from sysobjects,syscolumns
select ATA_DATE=convert(varchar(6),cast(a.intDate as datetime),112),TEU_COUNT=sum(b.TEU_COUNT)
from #intDate a left join VOYAGE b on datediff(day,a.ATA_DATE,b.intDate)=0
--group by datepart(month,cast(a.intDate as datetime))
group by convert(varchar(6),cast(a.intDate as datetime),112)