Я пытаюсь преобразовать плоскую таблицу в таблицу кросс-таблицы, используя функцию PIVOT. В приведенном ниже коде столбец «Численность персонала» преобразуется в 12 столбцов, используя столбец «MX» в качестве сводной.
Моя цель - добавить второе утверждение PIVOT, которое в основном выполняет ту же цель; Мне нужно добавить столбец «YTD» рядом со столбцом M12 в готовом продукте, который использует столбец 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