Advertisement
Advertisement
| 07.10.2008 at 10:38AM PDT, ID: 23554659 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: |
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER Procedure [dbo].[sproc_FTE_HeadCount]
(
@countdate datetime
)
AS
BEGIN
SET NOCOUNT ON
if OBJECT_ID('tempdb..#temp_emp_hist') is not null
begin
drop table #temp_emp_hist
end
create table #temp_emp_hist
(
coid char(12),
eeid char(12),
emplstatus char(1),
jobeffdate datetime,
reasondesc varchar(25),
datetimecreated datetime
)
insert into #temp_emp_hist
select b.ejhcoid,b.ejheeid,b.ejhemplstatus,case b.ejhreason when 'TRO' then dateadd(d,-1,b.ejhjobeffdate) else b.ejhjobeffdate end as ejhreason,
case b.ejhemplstatus when 'A' then c.jchdesc else d.tchdesc end as reasondesc, b.ejhdatetimecreated
from emphjob b left outer join jobchrsn c
on b.ejhreason = c.jchcode
left outer join trmreasn d
on b.ejhreason = d.tchcode
where ((b.ejhemplstatus = 'A' and b.ejhreason in ('CONV','100','101','TRI','TRB')) or
b.ejhemplstatus = 'T')
order by b.ejheeid,b.ejhjobeffdate
--Getting multiple associates in date order
if OBJECT_ID('tempdb..#temp_emp_hire_term') is not null
begin
drop table #temp_emp_hire_term
end
create table #temp_emp_hire_term
(
coid char(12),
eeid char(12),
dateoflasthire datetime,
dateoftermination datetime,
Activereasondesc varchar(25),
Termreasondesc varchar(25)
)
declare
@UPcoid char(12),
@UPeeid char(12),
@UPemplstatus char(1),
@UPjobeffdate datetime,
@UPreasondesc varchar(25),
@reccount int,
@Preeid char(12),
@Premplstatus char(1)
DECLARE emp_cursor CURSOR FOR
select a.coid,a.eeid,a.emplstatus,a.jobeffdate,a.reasondesc from #temp_emp_hist a
order by a.eeid,a.jobeffdate,a.datetimecreated
OPEN emp_cursor
FETCH NEXT FROM emp_cursor
INTO @UPcoid,@UPeeid,@UPemplstatus,@UPjobeffdate,@UPreasondesc
WHILE @@FETCH_STATUS = 0
BEGIN
set @reccount = 1
if @UPemplstatus = 'A'
begin
select @reccount = count(*) from #temp_emp_hire_term where coid = @UPcoid and eeid = @UPeeid and dateoflasthire = @UPjobeffdate
end
if @reccount = 0
begin
if (@UPeeid = @Preeid and @UPemplstatus = 'A' and @Premplstatus = 'T') or (@UPeeid <> @Preeid)
begin
insert into #temp_emp_hire_term
(coid,eeid,dateoflasthire,dateoftermination,Activereasondesc)
values
(@UPcoid,@UPeeid,@UPjobeffdate,null,@UPreasondesc)
end
end
else
begin
if @UPeeid = @Preeid and @UPemplstatus = 'T' and @Premplstatus = 'A'
begin
update #temp_emp_hire_term
set dateoftermination = @UPjobeffdate,
termreasondesc = @UPreasondesc
where coid = @UPcoid and
eeid = @UPeeid and
dateoftermination is null
end
end
set @Preeid = @UPeeid
set @Premplstatus = @UPemplstatus
FETCH NEXT FROM emp_cursor
INTO @UPcoid,@UPeeid,@UPemplstatus,@UPjobeffdate,@UPreasondesc
END
CLOSE emp_cursor
DEALLOCATE emp_cursor
select e.cmpcompanyname,
f.LocDesc,
b.ejhorglvl2 + ' ' + g.OrgDesc,
d.eecempno,
(rtrim(c.eepnamelast) + ', '+rtrim(c.eepnamefirst)+ case when c.eepnamemiddle is null then '' else ' '+rtrim(c.eepnamemiddle) end) as empname,
b.ejhjobcode,
b.ejhjobdesc,
b.ejhFullTimeOrPartTime,
case b.ejhFullTimeOrPartTime when 'F' then 1 when 'P' then 0.5 else 0 end as FTE,
dateoflasthire, -- Included for testing only
dateoftermination -- Included for testing only
from #temp_emp_hire_term a join
(select m.ejhcoid,m.ejheeid,m.ejhreason,m.ejhlocation,m.ejhemplstatus,m.ejhFullTimeOrPartTime,m.ejhOrgLvl2,m.ejhJobcode,m.ejhjobdesc
from emphjob m join
(
select ejhcoid,ejheeid,max(ejhdatetimecreated) as ejhdatetimecreated
from emphjob
where ejhemplstatus <> 'T' and ejhjobeffdate < dateadd(d,1,@countdate)
group by ejhcoid,ejheeid
) n
on m.ejhcoid = n.ejhcoid and
m.ejheeid = n.ejheeid and
m.ejhdatetimecreated = n.ejhdatetimecreated
) b
on a.coid = b.ejhcoid and
a.eeid = b.ejheeid
join emppers c
on a.eeid = c.eepeeid
join empcomp d
on a.coid = d.eeccoid and
a.eeid = d.eeceeid
join company e
on d.eeccoid = e.cmpcoid
join location f
on d.eecLocation = f.loccode
left outer join orglevel g
on b.ejhorglvl2 = g.orgcode and
g.orglvl = 2
where ((dateoflasthire <= @countdate and dateoftermination is null) or (@countdate between dateoflasthire and dateoftermination)) and
d.eecempno not like 'T%'
order by 1,2,5 asc
END
|