简体   繁体   中英

SQL Server 2005 Pivot against dynamic table to insert into table

I have a query that creates a dynamic table, and creates both fake columns and fake rows in order to fill a grid in an SSRS report.

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

I now need to pivot this into a table. I've tried using the following function to do a dynamic pivot, but when I tried to run it I get an error saying

Incorrect Syntax near the Keyword Exec.

Code:

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)

Execution of SQL that causes error:

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

Now I am not tied to this function to pivot this data, but one thing that prevents me from doing a standard pivot is that I will not know the values that make up the columns until runtime.

What is the best way of doing this?

Your dynamic pivot is working off an actual table, not a COMMON TABLE EXPRESSION (that you defined using the WITH keyword). A straightforward solution is to make a real table out of the CTE.

At the top, add

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

At the end of your query, add the into #EmployeeActivity clause:

Select *
into #EmployeeActivity
from EmployeeActivity;

Your dynamic pivot should work. Note: I think row needs to be in the select list.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM