1
我有一個創建一個動態表,以填補網格在SSRS報告同時創建假的列和假行的查詢。SQL服務器對動態表,2005年樞軸插入到表
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
我現在需要將它轉換爲表格。我使用下面的函數做一個動態支點試過了,但是當我試圖運行它,我得到一個錯誤說
Incorrect Syntax near the Keyword 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 '[email protected]+' as pivot_col,')
create table #pivot_columns (pivot_column varchar(100))
Select @sql='select distinct pivot_col from ('[email protected]+') as t'
insert into #pivot_columns
exec(@sql)
select @pivot=coalesce(@pivot+',','')+'['+pivot_column+']'from #pivot_columns
select @sql=
'
select * from
(
'[email protected]+'
) as t
pivot
(
'[email protected]+' for pivot_col in ('[email protected]+')
) as p
'
exec(@sql)
SQL的執行導致錯誤:
EXEC dynamic_pivot
'SELECT Employee_EmployeeID, Employee_FullName,StartDateTime from EmployeeActivity',
'JobAccount_TrackingID',
'Count(row)'
現在我沒有綁定到這個函數來轉移這些數據,但是有一件事阻止我做標準的pivot,那就是我不會知道直到運行時組成列的值。
什麼是這樣做的最佳方式?
謝謝,我會試試這個。 – CraigBob