具体也不清楚你要完成什么功能,象这种数据分析的处理最好能有存储过程来实现,我下面有个计算考勤加班计算的存储过程,你可以参考以下:
CREATE proc aSP_OTCL_ADD
as
Begin
declare @term datetime ,
@begindate datetime,
@enddate datetime
select @term=Term,@begindate=Begindate,@enddate=enddate
from skyattendprocess
select Badge,sum(Num) Num into #OTCL_BAL from ashift_details
where term between @begindate and @enddate
and TMWG='RTTM'
group by badge
--周六加班
select Badge,sum(Num) Num into #OT2_BAL from ashift_details
where term between @begindate and @enddate
and TMWG='OTTM'
and TMWGSUB in ('OT2U')
and datepart(weekday,term)=7
and Num<>0
group by badge
--非周六加班
select Badge,sum(Num) Num into #OT_BAL from ashift_details
where term between @begindate and @enddate
and TMWG='OTTM'
and not (TMWGSUB in ('OT2U')
and datepart(weekday,term)=7
)
and Num<>0
group by badge
insert aOTCL_BaL(Term,Badge)
select @term,badge
from #OTCL_BAL a
where not exists(select 1 from aOTCL_BaL b where a.badge=b.badge and datediff(month,b.term,@term)=0 )
insert aOTCL_BaL(Term,Badge)
select @term,badge
from #OT2_BAL a
where not exists(select 1 from aOTCL_BaL b where a.badge=b.badge and datediff(month,b.term,@term)=0 )
insert aOTCL_BaL(Term,Badge)
select @term,badge
from #OT_BAL a
where not exists(select 1 from aOTCL_BaL b where a.badge=b.badge and datediff(month,b.term,@term)=0 )
update a set a.RThis =0
from aOTCL_Bal a
where datediff(month,term,@term)=0
Update a set a.RThis=b.Num
from aOTCL_Bal a, #OTCL_BaL b
where a.badge=b.badge
and datediff(month,a.term,@term)=0
--and datediff(month,b.term,@term)=0
Update a set a.Rtotal=a.Rlast+a.RThis ,a.RBal =a.Rlast+a.RThis-a.RUsed
from aOTCL_Bal a
where datediff(month,a.term,@term)=0
--加班(周六加班)
Update a set a.O2This =0
from aOTCL_Bal a
where datediff(month,term,@term)=0
Update a set a.O2This=b.Num
from aOTCL_Bal a, #OT2_BaL b
where a.bad