select a.*,b.A,b.B
from (select TCD,FCD,SPCD,A=sum(A),B=sum(B)
from tb where YMD='200505031' group by TCD,FCD,SPCD)a
inner join (select TCD,FCD,SPCD,A=sum(A),B=sum(B)
from tb group by TCD,FCD,SPCD)b
on a.TCD=b.TCD and a.FCD=b.FCD and a.SPCD=b.SPCD
from (select TCD,FCD,SPCD,A=sum(A),B=sum(B)
from tb where YMD='200505031' group by TCD,FCD,SPCD)a
inner join (select TCD,FCD,SPCD,A=sum(A),B=sum(B)
from tb group by TCD,FCD,SPCD)b
on a.TCD=b.TCD and a.FCD=b.FCD and a.SPCD=b.SPCD
我的回复,尽可能为你分忧解难!
BLOG:blog.csdn.net/softj --欢迎光临,有更多信息等着你!
QQ群:8476022专研数据库 --大家进来聊一聊!
MSN:[email protected] --这不常用!
Mail:[email protected] --有什么问题可以和我来EMAIL!
=========================================================
TCD,
FCD,
SPCD,
YMD=convert(char(10),YMD,120),
sum(A),
sum(B)
from
[table]
where
datediff(mm,YMD,'2005-05-01')=0
group by
TCD,FCD,SPCD,convert(char(10),YMD,120)
with rollup
TCD,FCD,SPCD,convert(char(10),YMD,120)TCD, convert(char(10),YMD,120)
好像会产生些不用的记录吧