SQL Server 2005透视动态表插入表

时间:2012-10-12 03:00:41

标签: sql sql-server pivot-table

我有一个创建动态表的查询,并创建假列和虚假行以填充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

我现在需要将其转换为表格。我尝试使用以下函数来执行动态数据透视,但是当我尝试运行它时,我收到错误说

  

关键字执行附近的语法错误。

代码:

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)'

现在我没有依赖此函数来转移这些数据,但是阻止我做标准转轴的一件事是我不会知道构成列的值直到运行时。

这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

您的动态数据透视表正在使用实际表,而不是COMMON TABLE EXPRESSION(您使用WITH关键字定义的表)。一个简单的解决方案是从CTE中制作一个真实的表格。

在顶部,添加

if object_id('tempdb..#EmployeeActivity') is not null
drop table #EmployeeActivity;

在查询结束时,添加into #EmployeeActivity子句:

Select *
into #EmployeeActivity
from EmployeeActivity;

您的动态支点应该有效。注意:我认为row需要在选择列表中。

EXEC dynamic_pivot
'SELECT Employee_EmployeeID, Employee_FullName,StartDateTime,row from EmployeeActivity',
'JobAccount_TrackingID',
'Count(row)'