我的报告是前段时间有人告诉我的,它显示了一年中“评级”和“结果”列的数据。
目前脚本正在使用 Case 语句来实现这一点,但是我相信这就是 Pivot Function 的用途,并且可以节省一些代码行。任何人都可以帮助我更改逻辑以使用枢轴功能吗?当前逻辑:
Select
EmployeeID,
Employee,
--Datetype,
EmpGroupID,
PA,
EmpLevel,
ObjectiveName as Objective,
Objectiveid AS Objectiveid,
Weighting,
ReportingYear,
max(Case When r.DateType = 'M'
and Month(r.StartDate) = 1
and r.result IS NOT NULL
Then r.Result
When r.DateType = 'M'
and Month(r.StartDate) = 1
and r.result IS NULL
and r.rating is NOT NULL THEN COALESCE(rtovr.ratingShortDesc,rt.ratingShortDesc)
Else null
END) as Value_January_1,
MAX(Case When r.DateType = 'M'
and Month(r.StartDate) = 1
Then dbo.udfGetRating(case when r.RatingOverride is null
then r.Rating
else r.RatingOverride
end) + '|' + CAST(UserOverride AS CHAR(1)) + '|' + rt.ratingShortDesc
Else null
END) as Rating_January_1,
From #tblResults R
WHERE r.datetype IN ('M','Y')
...在选择中一直遵循该案例逻辑到 12 月。
表 #tblResult 上的示例数据:
EmployeeID Employee Datetype EmpGroupID PA EmpLevel ObjectiveName Objectiveid Weighting RPTYear ObjSortOrder ObjGrpSortOrder Result Rating RatingOverride startdate UserOverride
552940 test Q 664 Sr Rep 1 Overall Rating -1 1 2012 0 0 2 2.00 NULL 2012-01-01 0
552940 test Q 664 Sr Rep 1 Overall Rating -1 1 2012 0 0 2.39 2.00 NULL 2012-04-01 0