Вы находитесь на странице: 1из 15

USE [HuManEDGE]

GO
/****** Object: StoredProcedure [dbo].[SP_PM_Statistics_SubmittedDetail]
ipt Date: 7/9/2015 2:44:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Scr

/******************************************************************************
**
File:
**
Name: SP_PM_Statistics_SubmittedDetail
**
Desc:
**
**
This template can be customized:
**
**
Return values:
**
**
Called by:
**
**
Parameters:
**
Input
Output
**
------------------**
**
NOTE:
**
**
Auth:
**
Date:
*******************************************************************************
**
Change History
*******************************************************************************
**
Date:
Author:
Description:
20150417
KJCE
Shows all the ca
librator notes per Performance ID
**
20150422
KJCE
Added JobClass a
nd SubOrg filter to get specific
count in the detail page
**
20150519
KJCE
Added LastUpdate
dDate to show the current Notes on the top
**
20150526
KJCE
Added s.RN=1 to
get only the first series of notes
**
20150530
MFB
Added @p
SelectedEmp for search functionality
**
20150608
KJCE
Fix sorting
**
20150610
KJCE
Updates for scen
ario when choosing the vertical layer lower than the parent org id
**
20150615
KJCE
Removed filter S
tatus
**
20150615
KJCE
Include PA Forms
with Status = 98
**
20150630
KJCE
Exclude special
cycle
**
20150703
KJCE
Optimized query
*******************************************************************************/
--SP_PM_Statistics_SubmittedDetail '','6/30/2011','2930360','1','1','actual'

--ALTER PROCEDURE [dbo].[SP_PM_Statistics_SubmittedDetail]


DECLARE
@CyclePeriodStart varchar(10)='05/01/2014',
@CyclePeriodEnd varchar(10)='04/30/2015',
@ParentOrgID varchar(50)='DM0001',
@RatingType integer='1',
@Rating varchar(10)='ME',
@OrderField varchar(10)='actual',
@pEmployeeID varchar(20)=NULL
,@JobClass varchar(max)='110|120|90|100|30|80|10|70|20|',--20150422 KJCE
@SubOrgID VARCHAR(max) = '1|2|3|4|8|396|5|7|6|20|9|17|10|25|22|11|19|395|12|13
|470|18|14|23|16|21|314|340|15|370|371|380|68|28|42|48|69|34|61|372|438|439|70|3
06|50|51|341|381|58|65|62|131|32|55|132|91|477|416|425|71|52|60|397|78|35|38|398
|43|456|307|67|66|373|448|377|39|40|95|41|458|401|53|36|85|383|57|384|378|430|27
|92|86|87|88|89|26|30|45|362|93|46|94|473|90|466|44|351|56|96|97|98|29|37|99|337
|338|471|59|105|346|100|165|107|106|460|174|188|114|349|388|150|115|101|445|390|
126|127|123|145|441|387|389|339|426|427|428|481|173|353|452|109|342|125|326|148|
147|429|359|110|111|449|129|308|135|169|450|154|332|185|155|176|327|467|331|443|
347|183|151|149|158|472|161|415|113|459|344|102|170|104|432|431|440|166|463|464|
134|360|465|352|108|157|164|335|364|316|167|112|194|343|451|345|128|444|403|462|
168|130|124|442|186|333|334|187|189|191|190|184|195|196|175|193|156|350|172|446|
447|436|284|292|417|235|304|313|231|255|197|198|199|253|461|226|282|393|210|217|
201|233|248|469|285|286|437|271|204|203|202|200|392|265|216|232|212|275|220|211|
294|270|281|225|348|215|240|227|230|229|287|272|234|219|250|241|239|246|247|245|
243|244|214|283|205|224|295|206|409|256|236|420|418|228|264|262|261|269|280|238|
252|454|251|237|453|254|455|221|207|213|263|273|242|274|288|289|290|279|266|249|
391|410|218|291|209|257|424|299|369|367|302|300|298|423|421|366|419|301|303|336|
422|368|',-20150422 KJCE
@SelectedEmp varchar(20)='' -- 20150530 MFB
--AS

Declare @Level as int


set @Level = 0
SET NOCOUNT ON
--MJB 20150225
--ALWAYS SET LEVEL = 1
set @Level = 1
Update TB_PM_Assessment_Header_New
set Calibrated_Rating_Num = Final_Rating_num
where status='80'
and Calibrated_Rating_Num is null
Update TB_PM_Assessment_Header_New
set Calibrated_Rating = MAIN.SymBolrating2
From
(
Select X.Performance_ID ROW_ID2, X.Employee_ID,X.Calibrated_Rating_Num Calibr
ated_Rating_Num2,B.RangeFrom,RangeTo
,B.SymBolrating SymBolrating2
from
(Select * from

TB_PM_Assessment_Header_New A
where A.Performance_ID = (select top 1 Performance_ID from TB_PM_Assessment_H
eader_New
where CyclePeriod_To <= @CyclePeriodEnd
and Employee_ID=A.Employee_ID
and Status in ('110','80')
order by CyclePeriod_To desc,CyclePeriod_From
)
) X
left join (select Isnull(Calibration_RatingScale,RatingScale) RatingScale,For
m_Version from TB_PM_Form_Setup_DTL where Perf_Assessment_Val='40' ) RATING
on X.Form_Version=Rating.Form_Version
left join (select * from dbo.get_PM_PerformanceSymbol_Conversion('')) B
on (
(RATING.RatingScale=B.Rating_Code
and B.Section_Type_ID=Isnull((select top 1 Section_Type_ID from dbo.get_PM_Perf
ormanceSymbol_Conversion('') where Section_Type_ID='50'),'40')
)
OR
(Isnull(RATING.RatingScale,'') <> Isnull(B.Rating_Code,'') and B.Section_Type
_ID='50'
and B.Section_Type_ID=Isnull((select top 1 Section_Type_ID from dbo.get_PM_Perf
ormanceSymbol_Conversion('') where Section_Type_ID='50'),'40')
)
)
-- and X.Calibrated_Rating = B.SymBolrating
and Isnull(X.Calibrated_Rating_Num,0) between B.RangeFrom and B.RangeTo
) MAIN
where Performance_ID=MAIN.ROW_ID2
and Isnull(Calibrated_Rating,'') <> Isnull(MAIN.SymBolrating2,'')
/******************for org heads 20150525 KJCE*****************/
SELECT * INTO #OrgHeads FROM(
Select
MOVT.Emp
loyee_ID
--201507
03 KJCE only employee id is needed
--,EI.LN
F Name,
--(SELEC
T * FROM dbo.getOrganizationHeads(MOVT.Employee_ID,GETDATE(),ORG.Vertical_Layer,
GETDATE())) OrgHead,
--ORG.Or
ganization_ID,
--ORG.Sh
ort_Name,
--ORg.Ve
rtical_Layer
From
(
Select
Employee_ID,Position_ID,Movement_Type_ID, --20150703 KJCE
ROW_NUMBER() OVER (Partition By Employee_ID Order By [Start_Date] desc, SortSeq
desc) RN

From TB_
Employee_Movement_Info
Where
--Employ
ee_ID = @pEmployee_ID
--And
[Start_D
ate] <= GETDATE()
) MOVT
LEFT JOIN TB_Mas
ter_Position POS
on POS.Master_Po
sition_ID = MOVT.Position_ID
LEFT JOIN VW_EI_
PERSONAL_NAMES EI
ON EI.Employee_I
D=MOVT.Employee_ID
LEft Join TB_Mas
ter_Organization ORG
on ORG.Master_Or
ganization_ID = POS.Vertical_Assignment_ID
Where MOVT.RN =
1
AND MOVT.Movemen
t_Type_ID<>'280' --NOT YET SEPARATED
AND ORG.Organiza
tion_ID = @ParentOrgID
AND POS.Position
_Type_ID = '10' --OrgHead
AND MOVT.Employe
e_ID = (SELECT * FROM dbo.getOrganizationHeads(MOVT.Employee_ID,GETDATE(),ORG.Ve
rtical_Layer,GETDATE()))
) AAA
/************************************************/

--select * from TB_PM_Assessment_Header_New


select * into #tmp_EmpMov from
(
select
ROW_NUMBER() OVER (Partition By MOV.Employee_ID Order By MOV.S
tart_Date desc) RN, --20150703 KJCE to optimize
MOV.Employee_ID,
MOV.Movement_Type_ID, --20150703 KJCE
PER.Last_Name,
PER.First_Name,
PER.Middle_Name,
POS.Short_Name Position,
REF.Reference_Category_Description Job_Class,
MOV.Conglomerate_ID,
MOV.Company_ID,
MOV.Cluster_ID,
MOV.Division_ID,

MOV.Group_ID,
MOV.Department_ID,
MOV.Section_ID,
---MJB 20150225
MOV.VerticalLayer7,
MOV.VerticalLayer8,
MOV.VerticalLayer9,
MOV.VerticalLayer11,
MOV.VerticalLayer12
from
TB_Employee_Movement_Info MOV
left join TB_Master_Employment_Info EMP on
EMP.Employee_ID = MOV.Employee_ID
left join TB_Master_Personal_Info PER on
PER.Person_ID = EMP.Person_ID
left join TB_Master_Position POS on
POS.Master_Position_ID = MOV.Position_ID
left join TB_Master_Job_Level_Band JLB on
JLB.Band_Version_No = MOV.Band_Version_No
and JLB.Band_Version_Name = MOV.Band_Version_Name
and JLB.Job_Level_Band = MOV.Job_Level_Grade_ID
left join TB_Master_Reference REF on
REF.Reference_Category = '503'
and REF.Reference_Category_Key = JLB.Job_Class
where
--MOV.Movement_Type_ID <> '280'
--20150703 KJCE
--AND
JLB.Job_Class IN (select value from dbo.UTILfn_Split(@JobClass,'|'))-= @JobClass
-- ('10','100','110','20','30','70','80','90')
and MOV.Employment_Status_ID in ('10','20')
AND MOV.DeactivatedDate is null
and MOV.Start_Date <= @CyclePeriodEnd
and (MOV.End_Date is null or MOV.End_Date > @CyclePeriodEnd)
AND MOV.Employee_ID NOT IN (SELECT Employee_ID FROM #OrgHeads) --20150
525 KJCE
/*and MOV.Employee_Movement_Info_ID = (
Select
Top 1 Employee_Movement_Info_ID
From TB_Employee_Movement_Info
Where EmployeE_ID = MOV.Employee_ID
and DeactivatedDate is null
and Start_Date <= @CyclePeriodEnd
and (End_Date is null or End_Date > @CyclePeriodEnd)
Order By Start_Date Desc
)

*/

--AND MOV.Employee_ID <> '040006'


) A
where
RN=1 and
--20150703 KJCE

Movement_Type_ID <> '280'


--20150703 KJCE
/******************* END PART 1 ********************/

/********************added INDEX 20150703 KJCE*********************/


CREATE NONCLUSTERED INDEX INDEX_#tmp_EmpMov
ON [dbo].[#tmp_EmpMov] ([RN])
INCLUDE ([Employee_ID],[Conglomerate_ID],[Company_ID],[Cluster_ID],[Division_ID]
,[Group_ID],[Department_ID],[Section_ID],[VerticalLayer7],[VerticalLayer8],[Vert
icalLayer9],[VerticalLayer11],[VerticalLayer12])
/********************END INDEX**************************/

select * into #tmp_Org from


(
select
Master_Organization_ID,
convert(varchar(20),Organization_ID) Organization_ID,
Short_Name,
Vertical_Layer,
Parent_Organization_ID
from
TB_Master_Organization
where
CAST(Master_Organization_ID AS VARCHAR) IN (select value from dbo.UTILfn_Split(
@SubOrgID,'|')) OR @SubOrgID = '' AND --20150422 KJCE
(
@Level = 0 and Parent_Organization_ID = (select top 1 Master_Organization_ID f
rom TB_Master_Organization
Where Organization_ID = @ParentOrgID
and DeactivatedDate is null
and Start_Date <= @CyclePeriodEnd
and (End_Date is null or End_Date > @CyclePeriodEnd)
order by Start_Date desc)
or @Level = 1 and Master_Organization_ID = (select top 1 Master_Organization_I
D from TB_Master_Organization
Where Organization_ID = @ParentOrgID
and DeactivatedDate is null
and Start_Date <= @CyclePeriodEnd
and (End_Date is null or End_Date > @CyclePeriodEnd)
order by Start_Date desc)
)
--and isnull(StatisticViewControl,0) = 1
and Organization_Type_ID = '10' -- VERTICAL
and isnull(Parent_Organization_ID,0) <> 0
and DeactivatedDate is null
and Start_Date <= @CyclePeriodEnd
and (End_Date is null or End_Date > @CyclePeriodEnd)
) B

--MJB 20150225
--should cover children ORGS
-- this is like @level =0 and @level1 combined with expanded children ORG
CREATE TABLE #TB_Master_Organization

(
Master_Organization_ID int,
Organization_ID varchar(50),
BussUnitCode varchar(50),
Vertical_Layer varchar(10),
Parent_Organization_ID int,
Start_Date datetime,
End_Date datetime,
Short_Name varchar(200),
Long_Name varchar(500),
Organization_Type_ID varchar(5),
Control varchar(5),
LastUpdatedByID varchar(20),
LastUpdatedDate datetime,
DeactivatedByID varchar(20),
DeactivatedDate datetime,
)

----temp table for calibrator notes 20150417 KJCE


--;with cte as
select * into #NOTES FROM
(
select d.Performance_ID,
cal.Calibrator_ID,
ROW_NUMBER() OVER (Partition By d.Performance_ID Order By d.Crea
tedDate desc) RN,
CASE WHEN(
(SELECT RTRIM(
LTRIM(dbo.PM_RaterCalibratorNotes(
d.Performance_ID,cal.Calibrator_ID)
)
))) IS NULL
THEN ''
ELSE (SELECT RTRIM(
LTRIM(dbo.PM_RaterCalibratorNotes(
d.Performance_ID,cal.Calibrator_ID)
)
))
END Calibrator_Notes,
CAL.LastUpdatedDate --20150519 KJCE
from
TB_PM_Assessment_Header_New d
left join TB_PM_Calibrator_Notes cal
on d.Performance_ID = cal.Performance_ID
--where cal.Performance_ID=d.Performance_ID
) a
---temp table for calibrator notes
Insert into #TB_Master_Organization
(
Master_Organization_ID,
Organization_ID,
BussUnitCode,
Vertical_Layer,

Parent_Organization_ID,
Start_Date,
End_Date,
Short_Name,
Long_Name,
Organization_Type_ID,
Control,
LastUpdatedByID,
LastUpdatedDate,
DeactivatedByID,
DeactivatedDate
)
Select
ORG.Master_Organization_ID,
ORG.Organization_ID,
ORG.BussUnitCode,
ORG.Vertical_Layer,
ORG.Parent_Organization_ID,
ORG.Start_Date,
ORG.End_Date,
ORG.Short_Name,
ORG.Long_Name,
ORG.Organization_Type_ID,
ORG.Control,
ORG.LastUpdatedByID,
ORG.LastUpdatedDate,
ORG.DeactivatedByID,
ORG.DeactivatedDate
FROM
TB_MASTER_ORGANIZATION ORG
INNER JOIN #tmp_Org ORGACCESS
ON
(dbo.isOrganizationUnderOrganization(ORG.Master_Organization_ID,ORGACCES
S.Master_Organization_ID) = 1
OR ORG.Master_Organization_ID = ORGACCESS.Master_Organization_ID
)
--- END MJB 20150225
CREATE NONCLUSTERED INDEX [INDEX_#TB_Master_Organization]
ON [dbo].[#TB_Master_Organization] ([Master_Organization_ID])
INCLUDE ([Vertical_Layer])
select * into #tmp_EmpSubmit from
(
select
--DISTINCT
ROW_NUMBER() OVER (Partition By HDR.Employee_ID Order By ORG.Ver
tical_Layer ASC) RN, --20150703 KJCE to optimize
HDR.Performance_ID,
HDR.Employee_ID,
HDR.Final_Rating,
HDR.Final_Rating_Num,
isnull(HDR.Calibrated_Rating, HDR.Final_Rating) Calibrated_Ratin
g, --- MJB 20141202 ; if calib rating has no value, default to final rating
isnull(HDR.Calibrated_Rating_Num,HDR.Final_Rating_Num) Calibrate
d_Rating_Num, --- MJB 20141202 ; if calib rating has no value, default to fina
l rating
HDR.Status,
--HDR.EmployeesComments,
--HDR.SuperiorComments,

--HDR.BigBossComments,
--HDR.Form_Version,
HDR.Cycle_Version, --20150630 KJCE
--Isnull(FORMSETUP.Calibration_Preference,'10') Calibration_Pref
erence,
MOV.Last_Name,
MOV.First_Name,
MOV.Middle_Name,
MOV.Position,
MOV.Job_Class
,ORG.Vertical_Layer
,ORG.Master_Organization_ID Organization_ID
/*,
--Added by KJCE 20150320
CASE WHEN(
(SELECT RTRIM(
LTRIM(dbo.PM_RaterCalibratorNotes(
HDR.Performance_ID,@pEmployeeID)
)
))) IS NULL
THEN ''
ELSE (SELECT RTRIM(
LTRIM(dbo.PM_RaterCalibratorNotes(
HDR.Performance_ID,@pEmployeeID)
)
))
END Calibrator_Notes */
from
TB_PM_Assessment_Header_New HDR
left join #tmp_EmpMov MOV on
MOV.Employee_ID = HDR.Employee_ID
--MJB 20150225
Inner join #TB_Master_Organization ORG on
MOV.Conglomerate_ID = ORG.Master_Organization_ID
or MOV.Company_ID = ORG.Master_Organization_ID
or MOV.Cluster_ID = ORG.Master_Organization_ID
or MOV.Division_ID = ORG.Master_Organization_ID
or MOV.Group_ID = ORG.Master_Organization_ID
or MOV.Department_ID = ORG.Master_Organization_ID
or MOV.Section_ID = ORG.Master_Organization_ID
or MOV.VerticalLayer7 = ORG.Master_Organization_ID
or MOV.VerticalLayer8 = ORG.Master_Organization_ID
or MOV.VerticalLayer9 = ORG.Master_Organization_ID
or MOV.VerticalLayer11 = ORG.Master_Organization_ID
or MOV.VerticalLayer12 = ORG.Master_Organization_ID
--left join TB_PM_Form_Setup FORMSETUP
--on FORMSETUP.form_version= case when HDR.Form_Version='Summary' then (
select top 1 Form_Version from TB_PM_Assessment_Header_New
-where CyclePeriod_To <=HDR.CyclePeriod_To
-and Employee_ID=HDR.Employee_ID
-order by CyclePeriod_To desc
-)
-else HDR.Form_Ve

rsion end
where
(isnull(@CyclePeriodStart,'') = '' or CyclePeriod_From = @CyclePeriodSta
rt)
and cast(convert(varchar,CyclePeriod_To,101) as datetime) = cast(convert
(varchar,@CyclePeriodEnd,101) as datetime)
and Status in ('80','110', '98') -- For Division Head Review and Submitt
ed to HR status --20150615 KJCE
----and ORG.Organization_ID is not NULL --- MJB 20141202
AND ORG.Master_Organization_ID IN (select value from dbo.UTILfn_Split(@S
ubOrgID,'|')) OR @SubOrgID = ''
) C
WHERE Cycle_Version NOT IN (
SELECT Reference
_Category_Key FROM TB_Master_Reference
WHERE Reference_
Category = '1100'
) --20150630 KJCE
AND RN=1 --20150703 KJCE
select * from #tmp_EmpSubmit --return
Declare @Numeric_Rating_From varchar(10),
@Numeric_Rating_To varchar(10)
set @Numeric_Rating_From = '0.0'
set @Numeric_Rating_To = '0.0'
DECLARE @approverLevel varchar(10) = (select app.LevelNo from tb_master_approval
_setup app
inner join TB_Master_Organizatio
n org
on org.Vertical_Layer = app.Vert
ical_Layer
and TranCode = 'PERF'
where org.Organization_ID = @Par
entOrgID
AND org.End_Date IS NULL AND org
.DeactivatedDate IS NULL --20150630 KJCE
) -- 20150603 MFB
IF EXISTS (
SELECT 1 FROM TB_PM_CALIBRATOR_TRANSACTIONS WHERE Perfor
mance_ID
IN
(
SELECT Performance_ID FROM #tmp_EmpSubmit
WHERE (
(@RatingType = 0 and Final_Ratin
g = @Rating)
or
(@RatingType = 1 and Isnull(Cali
brated_Rating,Final_Rating) = @Rating)
)
)
)
BEGIN
select
--DISTINCT --20150703 KJCE
case when (select ISNULL(SUBMIT.Employee_ID, '') where (CAL.Status = '1

0' OR CAL.Status is null) and (Level_No = @approverLevel or Level_No is null) A


ND (CAL.Performance_ID IS NOT NULL)) != ''--20150630 KJCE
then 1
when (select ISNULL(SUBMIT.Employee_ID, '') where
(CAL.Status = '10' OR CAL.Status is null) and (Level_No = @approverLevel or Le
vel_No is null) ) = ''
then 0
else 0
end ForCalibrationFlag,
--20150610 KJCE
SUBMIT.Employee_ID,
SUBMIT.Performance_ID,
SUBMIT.Last_Name,
SUBMIT.First_Name,
SUBMIT.Middle_Name,
SUBMIT.Final_Rating ,
SUBMIT.Final_Rating_Num ,
SUBMIT.Calibrated_Rating ,
SUBMIT.Calibrated_Rating_Num ,
SUBMIT.Status ,
--SUBMIT.EmployeesComments,
--SUBMIT.SuperiorComments,
--SUBMIT.BigBossComments,
--SUBMIT.Form_Version,
--SUBMIT.Calibration_Preference,
SUBMIT.Job_Class,
SUBMIT.Position,
--t.Calibrator_Notes,
CASE WHEN (t.Calibrator_Notes='Add Notes')
then 'Add Notes'
ELSE
t.Calibrator_Notes
end Calibrator_Notes --20150417 KJCE
from #tmp_EmpSubmit SUBMIT
---20150417 KJCE
left join (
select Performance_ID, STUFF(
(SELECT ' ' + s.Calibrator_Notes FROM #NOTES s
WHERE s.performance_id = t.performance_id and s.RN=1 --20150526 KJCE
ORDER BY LastUpdatedDate DESC --20150519 KJCE
FOR XML PATH(''),type).value('(./text())[1]','varchar(max)'),1,1,'') AS Calibrat
or_Notes from #NOTES t --to include all notes of one Performance_ID
where t.RN=1
) t
on t.Performance_ID=SUBMIT.Performance_ID
--LEFT JOIN TB_PM_CALIBRATOR_TRANSACTIONS CAL
--ON CAL.Performance_ID = SUBMIT.Performance_ID
--20150417 KJCE
--20150608 KJCE
LEFT JOIN (
SELECT
Performance_ID,

Vertical_Layer,
Level_No,
[Status],
CASE WHEN ((SELECT Vertical_Layer FROM #
tmp_Org WHERE Organization_ID=@ParentOrgID)=(SELECT Vertical_Layer FROM #OrgHead
s))
THEN ROW_NUMBER() OVER (Partitio
n By Performance_ID Order By [Status] ASC, Level_No DESC)
ELSE
ROW_NUMBER() OVER (Partition By
Performance_ID Order By Level_No DESC, [Status] ASC)
END RN
FROM TB_PM_CALIBRATOR_TRANSACTIONS
--WHERE Vertical_Layer >= (SELECT Vertic
al_Layer FROM #OrgHeads)
) CAL
ON CAL.Performance_ID=SUBMIT.Performance
_ID
AND CAL.RN=1
--AND CAL.Level_No > 2

where
((@RatingType = 0 and Final_Rating = @Rating) --_Num between con
vert(float,isnull(@Numeric_Rating_From,0.0)) and convert(float,isnull(@Numeric_R
ating_To,0.0)))
or (@RatingType = 1 and Isnull(Calibrated_Rating,Final_Rating) =
@Rating)) --_Num between convert(float,isnull(@Numeric_Rating_From,0.0)) and co
nvert(float,isnull(@Numeric_Rating_To,0.0)))
--AND (( @SelectedEmp <> '' AND Submit.Employee_ID = @SelectedEm
p) OR (@SelectedEmp = ''))--20150530 MFB
--AND SUBMIT.Vertical_Layer = (SELECT Vertical_Layer FROM #OrgHeads)
--AND SUBMIT.Vertical_Layer IN (SELECT Vertical_Layer FROM TB_PM_CALIBRA
TOR_TRANSACTIONS WHERE Vertical_Layer >= (SELECT Vertical_Layer FROM #OrgHeads))
--20150608 KJCE
--AND Level_No <= 2
--and ISNULL(CAL.[Status],'10')='10' 20150615 KJCE
order by
--20150608 KJCE
ForCalibrationFlag DESC,
SUBMIT.Last_Name ASC,
SUBMIT.First_Name ASC,
/*case @OrderField
when 'actual' then Final_Rating_Num
when 'calibrated' then Calibrated_Rating_Num
when 'id' then ''
when 'position' then ''
when 'jobclass' then ''
when 'name' then ''
else ''
end desc,
case @OrderField
when 'actual' then ''
when 'calibrated' then ''
when 'id' then Employee_ID
when 'position' then Position
when 'jobclass' then Job_Class
when 'name' then ''

else ''
end asc,*/
Final_Rating ASC,
Final_Rating_Num DESC,
Calibrated_Rating ASC,
Calibrated_Rating_Num ASC,
SUBMIT.Status ASC,
--EmployeesComments,
--SuperiorComments,
--BigBossComments,
--Form_Version,
--Calibration_Preference,
Job_Class,
--Organization_ID,
SUBMIT.Performance_ID,
t.Calibrator_Notes,
Middle_Name,Position,
SUBMIT.Employee_ID
--20150608 KJCE
END
ELSE
select
DISTINCT
SUBMIT.Employee_ID,
SUBMIT.Performance_ID,
SUBMIT.Last_Name,
SUBMIT.First_Name,
SUBMIT.Middle_Name,
SUBMIT.Final_Rating ,
SUBMIT.Final_Rating_Num ,
SUBMIT.Calibrated_Rating ,
SUBMIT.Calibrated_Rating_Num ,
SUBMIT.Status ,
--SUBMIT.EmployeesComments,
--SUBMIT.SuperiorComments,
--SUBMIT.BigBossComments,
--SUBMIT.Form_Version,
--SUBMIT.Calibration_Preference,
SUBMIT.Job_Class,
SUBMIT.Position,
--t.Calibrator_Notes,
CASE WHEN (t.Calibrator_Notes='Add Notes')
then 'Add Notes'
ELSE
t.Calibrator_Notes
end Calibrator_Notes --20150417 KJCE
from #tmp_EmpSubmit SUBMIT
---20150417 KJCE
left join (
select Performance_ID, STUFF(
(SELECT ' ' + s.Calibrator_Notes FROM #NOTES s
WHERE s.performance_id = t.performance_id and s.RN=1 --20150526 KJCE
ORDER BY LastUpdatedDate DESC --20150519 KJCE
FOR XML PATH(''),type).value('(./text())[1]','varchar(max)'),1,1,'') AS Calibrat
or_Notes from #NOTES t --to include all notes of one Performance_ID

where t.RN=1
) t
on t.Performance_ID=SUBMIT.Performance_ID
--LEFT JOIN TB_PM_CALIBRATOR_TRANSACTIONS CAL
--ON CAL.Performance_ID = SUBMIT.Performance_ID
--20150417 KJCE
where
((@RatingType = 0 and Final_Rating = @Rating) --_Num between convert(fl
oat,isnull(@Numeric_Rating_From,0.0)) and convert(float,isnull(@Numeric_Rating_T
o,0.0)))
or (@RatingType = 1 and Isnull(Calibrated_Rating,Final_Rating) = @Ratin
g)) --_Num between convert(float,isnull(@Numeric_Rating_From,0.0)) and convert(f
loat,isnull(@Numeric_Rating_To,0.0)))
AND (( @SelectedEmp <> '' AND Submit.Employee_ID = @SelectedEmp) OR (@S
electedEmp = ''))
--20150530 MFB
order by
SUBMIT.Last_Name ASC,
SUBMIT.First_Name ASC,/*
case @OrderField
when 'actual' then Final_Rating_Num
when 'calibrated' then Calibrated_Rating_Num
when 'id' then ''
when 'position' then ''
when 'jobclass' then ''
when 'name' then ''
else ''
end desc,
case @OrderField
when 'actual' then ''
when 'calibrated' then ''
when 'id' then Employee_ID
when 'position' then Position
when 'jobclass' then Job_Class
when 'name' then ''
else ''
end asc,*/
Final_Rating ASC,
Final_Rating_Num DESC,
Calibrated_Rating ASC,
Calibrated_Rating_Num ASC,
SUBMIT.Status ASC,
--EmployeesComments,
--SuperiorComments,
--BigBossComments,
--Form_Version,
--Calibration_Preference,
Job_Class,
--CAL.Vertical_Layer,
--Organization_ID,
SUBMIT.Performance_ID,
t.Calibrator_Notes,
Middle_Name,
Position,
SUBMIT.Employee_ID
-*/
drop table #tmp_EmpMov
drop table #tmp_Org
drop table #tmp_EmpSubmit

drop table #TB_Master_Organization


drop table #NOTES
DROP TABLE #OrgHeads
-------------------------------------------------------------------------------------------------