I have following query which give me a row for each day earning for each employee.
Now i want to show those date rows as columns . my current query and its output is as follow.
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

This output need to be converted using pivot. i have tried by looking into some examples of pivot .
As per suggested answers to look into for solutions, now i have this query and its giving me error
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)
and error is as follow now
Must declare the table variable "@WorkingDays".
 
     
     
    