我有一个创建动态表的查询,并创建假列和假行以填充 SSRS 报告中的网格。
Declare @FieldLogID as int
Set @FieldLogID = 1018;
With EmployeeActivity as
(
SELECT distinct *, row_number() over(order by Employee_EmployeeID)as row
from
(
Select Employee_EmployeeID,
Employee_FullName,
Injured_Today_Custom,
--Case when JobAccount_TrackingID like'zzz%' then Null else JobAccount_TrackingID end as
JobAccount_TrackingID,
StartDateTime,
StopDateTime,
WorkHours
from
(
Select row_number() over (partition by Employee_EmployeeID order by JobAccount_TrackingID) row,*
from
(
SELECT DISTINCT
CustomizedFieldLogEmployee_1.Employee_EmployeeID,
CustomizedFieldLogEmployee_1.Employee_FullName,
NULL as Injured_Today_Custom,
CustomizedFieldLogJobAccount_1.JobAccount_TrackingID,
null as StartDateTime,
null as StopDateTime,
null as WorkHours
FROM
CustomizedAccount INNER JOIN CustomizedFieldLogJobAccount AS CustomizedFieldLogJobAccount_1 ON CustomizedAccount.AccountID = CustomizedFieldLogJobAccount_1.Account_AccountID
RIGHT OUTER JOIN CustomizedFieldLogEmployee AS CustomizedFieldLogEmployee_1 ON CustomizedFieldLogJobAccount_1.FieldLog_FieldLogID = CustomizedFieldLogEmployee_1.FieldLog_FieldLogID
WHERE
CustomizedFieldLogEmployee_1.FieldLog_FieldLogID = @FieldLogID
AND
CustomizedAccount.AppliesToEmployees = 1
Union
(Select 'aaaa01' EmployeeID,NULL,NULL,NULL,NULL,NULL,NULL
union Select 'aaaa02',NULL,NULL,NULL,NULL,NULL,NULL
union Select 'aaaa03',NULL,NULL,NULL,NULL,NULL,NULL
union Select 'aaaa04',NULL,NULL,NULL,NULL,NULL,NULL
union Select 'aaaa05',NULL,NULL,NULL,NULL,NULL,NULL)
Union
Select Distinct
Employee_EmployeeID,
Employee_FullName,
null as Injured_Today_Custom,
fake_TrackingID,
null as StartDateTime,
null as StopDateTime,
null as WorkHours
FROM
CustomizedFieldLogEmployee
cross join
(
Select 'zzz01' fake_TrackingID
union Select 'zzz02'
union Select 'zzz03'
union Select 'zzz04'
union Select 'zzz05'
union Select 'zzz06'
union Select 'zzz07'
union Select 'zzz08'
union Select 'zzz09'
union Select 'zzz10'
) a where FieldLog_FieldLogID = @FieldLogID
) b
) c where Row<=9
--order by Employee_EmployeeID,JobAccount_TrackingID
)d
);
Select * from EmployeeActivity
我现在需要把它转成表格。我尝试使用以下函数进行动态旋转,但是当我尝试运行它时,我收到一条错误消息
关键字 Exec 附近的语法不正确。
代码:
create procedure dynamic_pivot
(
@select varchar(2000),
@PivotCol varchar(100),
@Summaries varchar(100)
) as
declare @pivot varchar(max), @sql varchar(max)
select @select =replace(@select,'select ','select '+@PivotCol+' as pivot_col,')
create table #pivot_columns (pivot_column varchar(100))
Select @sql='select distinct pivot_col from ('+@select+') as t'
insert into #pivot_columns
exec(@sql)
select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columns
select @sql=
'
select * from
(
'+@select+'
) as t
pivot
(
'+@Summaries+' for pivot_col in ('+@pivot+')
) as p
'
exec(@sql)
执行导致错误的 SQL:
EXEC dynamic_pivot
'SELECT Employee_EmployeeID, Employee_FullName,StartDateTime from EmployeeActivity',
'JobAccount_TrackingID',
'Count(row)'
现在我没有绑定到这个函数来旋转这些数据,但是阻止我执行标准旋转的一件事是,直到运行时我才会知道组成列的值。
这样做的最佳方法是什么?