1. 程式人生 > 其它 >向臨時表插入臨時表的資料

向臨時表插入臨時表的資料

向臨時表插入臨時表的資料的格式:
insert into #temp1(hztj,jgsh,jgsmc,zsum,z1,z2,z3,fsum,f1,f2,f3)
select * from #temp2 group by rwdh,jgsh,jgsmc

CREATE proc [dbo].[z_RE_DYRW_FX_NEW]
 @begdate datetime,
 @enddate datetime,
 @hztj  varchar(10),
 @gzzxh varchar(10)
as
create table #z_BI_dy_yqwclkh(
  hztj  varchar(10) null,
  jgsh  varchar(15) null,
  jgsmc varchar(80) null,
  zsum  decimal(18,2) null,
  z1    decimal(18,2) null,
  z2    decimal(18,2) null,
  z3    decimal(18,2) null,
  fsum  decimal(18,2) null,
  f1    decimal(18,2) null,
  f2    decimal(18,2) null,
  f3    decimal(18,2) null
)
select a.rwdh,c.jgsh,c.jgsmc,b.hh,c.gxh,c.SJJLH,c.SJJL,c.CPJLH,c.CPJL,
       wcqk=case when c.jhwc>=isnull(c.sjwc,convert(varchar(10),getdate(),120)) and c.sjwc is not null 
                 then  '按期完成' 
                 when c.jhwc<isnull(c.sjwc,convert(varchar(10),getdate(),120)) and c.sjwc is not null 
                 then  '超期完成' 
                 when c.jhwc<isnull(c.sjwc,convert(varchar(10),getdate(),120)) and c.sjwc is null
                 then  '超期未完成' 
                 when c.jhwc>=isnull(c.sjwc,convert(varchar(10),getdate(),120)) and c.sjwc is null 
                 then  '未超期未完成' end
into #tmp_dywclkh
from dyrwh a
inner join dyrwb b on a.djlsh=b.djlsh
inner join dyrws c on a.djlsh=c.djlsh and b.djbth=c.djbth
where  a.gbbz='N' and shbz = 'Y'
  and  a.XDRQ>=@begdate and a.XDRQ<=@enddate
  and  a.gzzxh like @gzzxh+'%'
  and  isnull(c.gxzt,'0-自由')<>'9-終止'

insert into #z_BI_dy_yqwclkh(hztj,jgsh,jgsmc,zsum,z1,z2,z3,fsum,f1,f2,f3)
select '按加工廠',jgsh,jgsmc,
       zsum=count(distinct rwdh),
       z1=  count(distinct(case when wcqk = '按期完成' then rwdh end)),
       z2=  count(distinct(case when wcqk = '超期完成' then rwdh end)),
       z3=  count(distinct(case when wcqk = '超期未完成' then rwdh end)),
       fsum=sum(1),
       f1=  sum((case when wcqk = '按期完成' then 1 end)),
       f2=  sum((case when wcqk = '超期完成' then 1 end)),
       f3=  sum((case when wcqk = '超期未完成' then 1 end))
from #tmp_dywclkh
group by rwdh,jgsh,jgsmc

insert into #z_BI_dy_yqwclkh(hztj,jgsh,jgsmc,zsum,z1,z2,z3,fsum,f1,f2,f3)
select '按生技經理',SJJLH,SJJL,
       zsum=count(distinct rwdh),
       z1=  count(distinct(case when wcqk = '按期完成' then rwdh end)),
       z2=  count(distinct(case when wcqk = '超期完成' then rwdh end)),
       z3=  count(distinct(case when wcqk = '超期未完成' then rwdh end)),
       fsum=sum(1),
       f1=  sum((case when wcqk = '按期完成' then 1 end)),
       f2=  sum((case when wcqk = '超期完成' then 1 end)),
       f3=  sum((case when wcqk = '超期未完成' then 1 end))
from #tmp_dywclkh
group by rwdh,SJJLH,SJJL

insert into #z_BI_dy_yqwclkh(hztj,jgsh,jgsmc,zsum,z1,z2,z3,fsum,f1,f2,f3)
select '按駐廠員',CPJLH,CPJL,
       zsum=count(distinct rwdh),
       z1=  count(distinct(case when wcqk = '按期完成' then rwdh end)),
       z2=  count(distinct(case when wcqk = '超期完成' then rwdh end)),
       z3=  count(distinct(case when wcqk = '超期未完成' then rwdh end)),
       fsum=sum(1),
       f1=  sum((case when wcqk = '按期完成' then 1 end)),
       f2=  sum((case when wcqk = '超期完成' then 1 end)),
       f3=  sum((case when wcqk = '超期未完成' then 1 end))
from #tmp_dywclkh
group by rwdh,CPJLH,CPJL

select hztj,jgsh,jgsmc,
       zsum=sum(zsum),z1=sum(z1),z2=sum(z2),z3=sum(z3),zwcl=case when sum(zsum) <>0 then round(sum(z1)/sum(zsum)*100,2) else 0 end,z4=sum(zsum)-sum(z1)-sum(z2),
       fsum=sum(fsum),f1=sum(f1),f2=sum(f2),f3=sum(f3),fwcl=case when sum(fsum) <>0 then round(sum(f1)/sum(fsum)*100,2) else 0 end,f4=sum(fsum)-sum(f1)-sum(f2)
from #z_BI_dy_yqwclkh 
where hztj like @hztj+'%'
group by hztj,jgsh,jgsmc
order by hztj

drop table #tmp_dywclkh
drop table #z_BI_dy_yqwclkh