select a.*
,b.月累计,b.月
,c.年累计,c.年
from(
select Item,Color
,日累计=sum(Quantity)
,日=convert(char(10),日期,120)
from 表
group by Item,Color,convert(char(10),日期,120)
)a,(
select Item,Color
,月累计=sum(Quantity)
,月=convert(char(7),日期,120)
from 表
group by Item,Color,convert(char(7),日期,120)
)b,(
select Item,Color
,年累计=sum(Quantity)
,年=convert(char(10),日期,120)
from 表
group by Item,Color,convert(char(4),日期,120)
)c
where a.Item=b.Item and a.Color=b.Color
and a.Item=c.Item and a.Color=c.Color
and a.日 like b.月+'-__'
and b.月 like a.年+'-__'

解决方案 »

  1.   

    select a.*
    ,b.月累计,b.月
    ,c.年累计,c.年
    from(
    select Item,Color
    ,日累计=sum(Quantity)
    ,日=convert(char(10),日期,120)
    from 表
    group by Item,Color,convert(char(10),日期,120)
    )a,(
    select Item,Color
    ,月累计=sum(Quantity)
    ,月=convert(char(7),日期,120)
    from 表
    group by Item,Color,convert(char(7),日期,120)
    )b,(
    select Item,Color
    ,年累计=sum(Quantity)
    ,年=convert(char(10),日期,120)
    from 表
    group by Item,Color,convert(char(4),日期,120)
    )c
    where a.Item=b.Item and a.Color=b.Color
    and a.Item=c.Item and a.Color=c.Color
    and a.日 like b.月+'-__'
    and b.月 like c.年+'-__'  --写错别名