我正在尝试使用PIVOT函数将平面表转换为交叉表。在下面的代码中,以“ MX”列为枢轴,将“ Headcount”列转换为12列。
我的目标是添加第二条基本完成相同目标的PIVOT语句;我需要在最终产品的M12列旁边添加一个“ YTD”列,该平面使用平面文件中的YTD列,并提供平均人数。请参见下面的代码。
我将整个代码都包含在表格中,但是请集中注意/ *下方代码* /注释行下面的代码。
DECLARE @queryDate1 datetime SET @queryDate1 = '03/31/2019';
DECLARE @queryDate2 datetime set @queryDate2 = EOMONTH(@queryDate1,-1);
DECLARE @queryDate3 datetime SET @queryDate3 = EOMONTH(@queryDate1,-2);
DECLARE @queryDate4 datetime set @queryDate4 = EOMONTH(@queryDate1,-3);
DECLARE @queryDate5 datetime SET @queryDate5 = EOMONTH(@queryDate1,-4);
DECLARE @queryDate6 datetime set @queryDate6 = EOMONTH(@queryDate1,-5);
DECLARE @queryDate7 datetime SET @queryDate7 = EOMONTH(@queryDate1,-6);
DECLARE @queryDate8 datetime set @queryDate8 = EOMONTH(@queryDate1,-7);
DECLARE @queryDate9 datetime SET @queryDate9 = EOMONTH(@queryDate1,-8);
DECLARE @queryDate10 datetime set @queryDate10 = EOMONTH(@queryDate1,-9);
DECLARE @queryDate11 datetime SET @queryDate11 = EOMONTH(@queryDate1,-10);
DECLARE @queryDate12 datetime set @queryDate12 = EOMONTH(@queryDate1,-11);
With tmpTblTTM as
(
SELECT
convert(varchar(10),@queryDate12,101) as [M1]
,convert(varchar(10),@queryDate11,101) as [M2]
,convert(varchar(10),@queryDate10,101) as [M3]
,convert(varchar(10),@queryDate9,101) as [M4]
,convert(varchar(10),@queryDate8,101) as [M5]
,convert(varchar(10),@queryDate7,101) as [M6]
,convert(varchar(10),@queryDate6,101) as [M7]
,convert(varchar(10),@queryDate5,101) as [M8]
,convert(varchar(10),@queryDate4,101) as [M9]
,convert(varchar(10),@queryDate3,101) as [M10]
,convert(varchar(10),@queryDate2,101) as [M11]
,convert(varchar(10),@queryDate1,101) as [M12]
),
DateTable as (
SELECT v.* from tmpTblTTM T
cross apply
(
values
('M12',month(T.M12),YEAR(t.m12)),
('M11',month(T.M11),YEAR(t.m11)),
('M10',month(T.M10),YEAR(t.m10)),
('M9',month(T.M9),YEAR(t.m9)),
('M8',month(T.M8),YEAR(t.m8)),
('M7',month(T.M7),YEAR(t.m7)),
('M6',month(T.M6),YEAR(t.m6)),
('M5',month(T.M5),YEAR(t.m5)),
('M4',month(T.M4),YEAR(t.m4)),
('M3',month(T.M3),YEAR(t.m3)),
('M2',month(T.M2),YEAR(t.m2)),
('M1',month(T.M1),YEAR(t.m1))
)
v(MX,Month,Year))
/* FOCUS ON THE CODE BELOW */
Select
Prop,
'' as Pay_Group,
Center,
Job,
isnull(M12,0) as Current_Month,
isnull(M1,0) as M1,
isnull(M2,0) as M2,
isnull(M3,0) as M3,
isnull(M4,0) as M4,
isnull(M5,0) as M5,
isnull(M6,0) as M6,
isnull(M7,0) as M7,
isnull(M8,0) as M8,
isnull(M9,0) as M9,
isnull(M10,0) as M10,
isnull(M11,0) as M11,
isnull(M12,0) as M12,
ISNULL(YTD,0) as YTD /*<---- This column should show the average of Headcount using column YTD as criteria */
from (
select
dt.MX MX,
Prop,
Home_Dept_Code Center,
Job_Code Job,
case when du.Month = 3 and du.Year = 2019 then 1 else 0 end as YTD,
Count(DISTINCT Tax_ID) HeadCount
from ee_Database_Uploaded du join DateTable dt on du.Month = dt.Month and du.Year = dt.Year
Where
Employee_Status <>'T'
and du.Year >= 2018
Group by
du.Month,
du.Year,
dt.MX,
Prop,
Pay_Group,
Home_Dept_Code,
Job_Code
)tbl1
PIVOT
(
Avg(Headcount) for MX in (M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12) /*<----This statement works just fine and transposes column MX into twelve columns */
Avg(Headcount) for YTD in (1) /*<----I have added this statement to convert column Headcount into one column but it does not work */
) as TAb2