客æ·æåºéæ±ï¼é对æä¸ååç»å ä¸å°è®¡ï¼å计æ±æ»ãç½ä¸æ¾äºä¸äºæå
³SQLå å计çè¯å¥ãé½ä¸æ¯å¾çæ³ãå³å®èªå·±å¨æåã
æè·¯æä¸ä¸ªï¼
1.å¾å¤ç¨GROUPPINGåROLLUPæ¥å®ç°ã
ä¼ç¹ï¼å®ç°ä»£ç ç®æ´ï¼è¦æ±å¯¹GROUPPINGåROLLUPå¾æ·±çç解ã
缺ç¹ï¼ä½çæ¬çSql Serverä¸æ¯æã
2.游æ å®ç°ã
ä¼ç¹ï¼æè·¯é»è¾ç®æ´ã
缺ç¹ï¼å¤æåä½æã
3.å©ç¨ä¸´æ¶è¡¨ã
ä¼ç¹ï¼æè·¯é»è¾ç®æ´ï¼æ§è¡æçé«ãSQLå®ç°ç®åã
缺ç¹ï¼æ°æ®é大æ¶èç¨å
å.
综åä¸ç§æ
åµï¼å³å®âå©ç¨ä¸´æ¶è¡¨âå®ç°ã
å®ç°ææ
å ä¸å°è®¡ï¼å计åææ
SQLè¯å¥
å¤å¶ä»£ç 代ç å¦ä¸:
select * into #TB from TB
select * into #TB1 from #TB where 1<>1
select distinct zcxt into #TBype from #TB order by zcxt
select identity(int,1,1) fid,zcxt into #TBype1 from #TBype
DECLARE @i int
DECLARE @k int
select @i=COUNT(*) from #TBype
set @k=0
DECLARE @strfname varchar(50)
WHILE @k < @i
BEGIN
Set @k =@k +1
select @strfname=zcxt from #TBype1 where fid =@k
set IDENTITY_INSERT #TB1 ON
insert into #TB1(fid,qldid,fa_cardid,ztbz,fa_name,model,i_number,gzrq,zcyz,ljzj,jz,sybm,zcxt,fa_ljjzzb)
select fid,qldid,fa_cardid,ztbz,fa_name,model,i_number,gzrq,zcyz,ljzj,jz,sybm,zcxt,fa_ljjzzb from
(
select * from #TB where zcxt=@strfname
union all
select 0 fid,'' qldid,'' fa_cardid,'' ztbz,'å°è®¡' fa_name,'' model,sum(i_number) as i_number,'' gzrq,sum(CAST(zcyz as money)) as zcyz,sum(CAST(ljzj as money)) as ljzj,sum(CAST(jz as money)) as jz,'' sybm,'' zcxt,Sum(fa_ljjzzb) as fa_ljjzzb
from #TB where zcxt=@strfname
group by ztbz
) as B
set IDENTITY_INSERT #TB1 off
END
select qldid,fa_cardid,zcxt,fa_name,model,i_number,gzrq,zcyz,ljzj,jz,sybm,ztbz,fa_ljjzzb from #TB1
union all
select '' qldid,'' fa_cardid,'' ztbz,'å计' fa_name,'' model,sum(i_number) as i_number,'' gzrq,sum(CAST(zcyz as money)) as zcyz,sum(CAST(ljzj as money)) as ljzj,sum(CAST(jz as money)) as jz,'' sybm,'' zcxt,Sum(fa_ljjzzb) as fa_ljjzzb
from #TB
drop table #TB1
drop table #TBype1
drop table #TBype
drop table #TB
æ©å±æ¹è¿
å¯ä»¥æ¹åæä¸ä¸ªéç¨çæ·»å å计å°è®¡çåå¨è¿ç¨ã