你试过了吗?这些例子没什么问题,但我做不到。我不想让人觉得你吹毛求疵,但为什么?我只是在这里处理过类似的问题,@hubsonbropa pivot示例的问题是,它们用于静态列数,但在我的示例中,列数总是随着日期范围的变化而变化。您不能从动态SQL语句中引用

你试过了吗?这些例子没什么问题,但我做不到。我不想让人觉得你吹毛求疵,但为什么?我只是在这里处理过类似的问题,@hubsonbropa pivot示例的问题是,它们用于静态列数,但在我的示例中,列数总是随着日期范围的变化而变化。您不能从动态SQL语句中引用,sql,sql-server,pivot,Sql,Sql Server,Pivot,你试过了吗?这些例子没什么问题,但我做不到。我不想让人觉得你吹毛求疵,但为什么?我只是在这里处理过类似的问题,@hubsonbropa pivot示例的问题是,它们用于静态列数,但在我的示例中,列数总是随着日期范围的变化而变化。您不能从动态SQL语句中引用表变量@WorkingDays。感谢您的链接,我已经解决了查询及其对我的作用。我还发布了结果查询作为答案。 declare @StartDate datetime,@EndDate datetime,@CompanyId int set @St


你试过了吗?这些例子没什么问题,但我做不到。我不想让人觉得你吹毛求疵,但为什么?我只是在这里处理过类似的问题,@hubsonbropa pivot示例的问题是,它们用于静态列数,但在我的示例中,列数总是随着日期范围的变化而变化。您不能从动态SQL语句中引用表变量@WorkingDays。感谢您的链接,我已经解决了查询及其对我的作用。我还发布了结果查询作为答案。
declare @StartDate datetime,@EndDate datetime,@CompanyId int
set @StartDate='01/01/2013'
set @EndDate='01/31/2013'
set @CompanyId=3


;with d(date) as (
  select cast(@StartDate as datetime)
  union all
  select date+1
  from d
  where date < @EndDate
  )
select distinct d.date CDate,E.EmployeeId,Earning.EarningDescription,Earning.EarningId
,E.FirstName + ' ' + E.MiddleName + ' ' + E.LastName AS EmployeeName
from d,Employee as E
inner join Earning on E.CompanyId=Earning.CompanyId
where E.CompanyId=@CompanyId and Earning.IsOnTimeCard=1 and Earning.IsHourly=1 
order by EmployeeId,CDate,EarningId
declare @StartDate datetime,@EndDate datetime,@CompanyId int,@cols AS NVARCHAR(MAX),@query  AS NVARCHAR(MAX)
set @StartDate='01/01/2013'
set @EndDate='01/31/2013'
set @CompanyId=3



declare @WorkingDays Table 
 (
   WDate smalldatetime
 )


;with d(date) as (
  select cast(@StartDate as datetime)
  union all
  select date+1
  from d
  where date < @EndDate
  )
  insert into @WorkingDays select  d.date from d

  SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(wd.WDate) 
            FROM @WorkingDays wd
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

  PRINT @cols

 set @query = ' 
SELECT
*
FROM
(
    select distinct WDate CDate,E.EmployeeId,Earning.EarningDescription,Earning.EarningId

    from  @WorkingDays  ,Employee as E
    inner join Earning on E.CompanyId=Earning.CompanyId
    where E.CompanyId=@CompanyId and Earning.IsOnTimeCard=1 and Earning.IsHourly=1 
) src
PIVOT
(
    MIN(src.EarningId)
    FOR src.CDate IN ('+@cols+')
) AS PivotedView '

PRINT (@query)
execute(@query)
 select '01/01/2013' Cdate, 19 EmployeeID,'regular' EarningDescription, 21 EarningID, CAST('rebecca smith' as varchar(20)) Employeename INTO #temp union all select
 '01/01/2013', 19,'OverTime', 21, 'rebecca smith'  union all select
 '01/01/2013', 19,'DoubleOvertime', 22, 'rebecca smith'  union all select
 '01/01/2013', 19,'regular', 23, 'rebecca smith'  union all select
 '01/01/2013', 19,'vacation', 104, 'rebecca smith'  union all select
 '01/01/2013', 19,'Travel', 105, 'rebecca smith'  union all select
 '02/01/2013', 19,'regular', 21, 'rebecca smith'  union all select
 '02/01/2013', 19,'OverTime', 21, 'rebecca smith'  union all select
 '02/01/2013', 19,'DoubleOvertime', 22, 'rebecca smith'  union all select
 '02/01/2013', 19,'regular', 23, 'rebecca smith'  union all select
 '02/01/2013', 19,'vacation', 104, 'rebecca smith'  union all select
 '02/01/2013', 19,'Travel', 105, 'rebecca smith'   union all select
 '03/01/2013', 19,'regular', 21, 'rebecca smith'  union all select  
 '03/01/2013', 19,'OverTime', 21, 'rebecca smith'  union all select
 '03/01/2013', 19,'DoubleOvertime', 22, 'rebecca smith'  union all select
 '03/01/2013', 18,'regular', 23, 'ganesh'  union all select
 '03/01/2013', 18,'vacation', 104, 'ganesh'  union all select
 '03/01/2013', 18,'Travel', 105, 'ganesh'  
 declare @StartDate datetime,@EndDate datetime,@CompanyId int
 set @StartDate='01/01/2013'
 set @EndDate='01/31/2013'
 set @CompanyId=3
 ;with d(date) as (
   select cast(@StartDate as datetime) as date
   union all
   select date+1
   from d
   where date < @EndDate
   )

   select * INTO #dates FROM d
   declare @dates varchar(max), @datecolumn varchar(max) 
   select @dates=COALESCE(@dates+',','')+'['+convert(varchar(MAX),date,103)+']' from #dates
   print @dates
   EXEC(
  ' SELECT * FROM (
      select distinct  convert(varchar(MAX),CDate,103) CDate,E.EmployeeId,e.EarningDescription,e.EarningId
      , EmployeeName
      from #temp e
      ) as x
      PIVOT  (
      MAX(EmployeeName)
 FOR CDate IN ('+@dates+')
 ) as pvt order by EmployeeId,EarningId')
declare @StartDate datetime,@EndDate datetime,@CompanyId int,@cols AS NVARCHAR(MAX),@query  AS NVARCHAR(MAX)
set @StartDate='01/01/2013'
set @EndDate='01/31/2013'
set @CompanyId=3



 Create table #t
   (
     WDate smalldatetime
   )





;with d(date) as (
  select cast(@StartDate as datetime)
  union all
  select date+1
  from d
  where date < @EndDate
  )
  insert into #t select  d.date from d

  SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(wd.WDate) 
            FROM #t wd
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

  PRINT @cols

 set @query = ' 
SELECT
*
FROM
(
    select distinct WDate CDate,E.EmployeeId,Earning.EarningDescription,Earning.EarningId
    ,E.FirstName +'  + '''' + ' ''' + '+ E.MiddleName +'  + '''' + ' ''' + '+ E.LastName AS EmployeeName
    from  #t  ,Employee as E
    inner join Earning on E.CompanyId=Earning.CompanyId
    where E.CompanyId='+ CAST (@CompanyId as nvarchar(50)) +' and Earning.IsOnTimeCard=1 and Earning.IsHourly=1 
) src
PIVOT
(
    MIN(EarningDescription)
    FOR src.CDate IN ('+@cols+')
) AS PivotedView order by EmployeeId,EarningId '

PRINT (@query)
execute(@query)

drop table #t