日期:2014-05-18 浏览次数:20570 次
create table tb(col1 varchar(1),col2 decimal(10,0)) insert into tb select 'A',1000 insert into tb select 'A',200 insert into tb select 'B',800 drop FUNCTION dbo.f_str CREATE FUNCTION dbo.f_str(@col1 varchar(10)) RETURNS varchar(100) AS BEGIN DECLARE @re varchar(100) SET @re='' SELECT @re=@re+'+'+CAST(col2 as varchar) FROM tb WHERE col1=@col1 RETURN(STUFF(@re,1,1,'')+'=') END select col1,dbo.f_str(col1)+ltrim(sum(col2)) as hb,count(*) as [count] from tb group by col1
------解决方案--------------------
select count(sjzb.SJZB),sum(t1.htzj),sum(t2.SSZJ)
from sjzb,(select count(*) as count1,sum(htzj) as htzj from SSMC group SJBH),(select count(*) as count2,sum(SSZJ) as SSZJ from JSSJ group SJBH)
where sjzb.SJZT='出具审计报告' and sjzb.sjbh=t1.sjbh and t1.sjbh.t2.sjbh and t1.count1=t2.count2
------解决方案--------------------
只能做到这步了
select count(*), sum(htzj), sum(sszj) from (select sjbh, htzj, sszj from sjzb where sjbh in (select a.sjbh from (select SJBH, count(sjbh) as sjbhcount from SSMC group by SJBH union select SJBH, -1 * count(sjbh) as sjbhcount from JSSJ group by SJBH) as a inner join sjzb s on a.sjbh = s.sjbh where s.SJZT = '出具审计报告' group by a.sjbh having sum(sjbhcount) = 0 )) b
------解决方案--------------------
--1、总表数据(SJZB) declare @SJZB table (SJBH varchar(12),SJXS varchar(4),HTZJ decimal(18,2),SSZJ decimal(18,2),SJZT varchar(15)) insert into @SJZB values('NS-2007-001','結算',490948.23,665614.42,'出具審計報告') insert into @SJZB values('NS-2007-002','決算',327298.82,443742.88,'出具審計報告') insert into @SJZB values('NS-2007-003','預算',163649.41,221871.54,'審計結果確認') insert into @SJZB values('NS-2007-004','結算',163649.41,221871.54,'出具審計報告') insert into @SJZB values('NS-2007-005','結算',163649.41,221871.54,'出具審計報告') select * from @SJZB --2、送审数据(SSMC) declare @SSMC table (id int,SJBH varchar(12),HTZJ decimal(18,2),SSZJ decimal(18,2)) insert into @SSMC values(1,'NS-2007-001',163649.4