Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to get dates as header (Dynamic Header)

Author  Topic 

naresh0407
Starting Member

30 Posts

Posted - 2010-11-09 : 11:36:15
Hi All,

How to get dates as header in the result .
In the below query i am getting header as Second Week instead of that i need to get 8/8/2010 as Header(Dynamic Dates).

convert(varchar,dbo.FDiv(Count(Case When Delta>='0' Then EmpNo end),@TotalEmp)*100)+'%' as 'Second Week' FROM SAR_WeekData where WeekStartDate>='8/8/2010'



Declare @test table (empno int,MatchingOff varchar(10),atnDate datetime)
Insert into @test(empno,MatchingOff,atnDate)
select '101','Off','8/1/2010' union all
select '101','17:00','8/2/2010' union all
select '101','17:00','8/3/2010' union all
select '101','16:00','8/4/2010' union all
select '101','16:00','8/5/2010' union all
select '101','16:00','8/6/2010' union all
select '101','16:00','8/7/2010' union all
select '101','16:00','8/8/2010' union all
select '101','Off','8/9/2010' union all
select '101','17:00','8/10/2010' union all
select '101','17:00','8/11/2010' union all
select '101','16:00','8/12/2010' union all
select '101','16:00','8/13/2010' union all
select '101','16:00','8/14/2010' union all
select '101','16:00','8/15/2010' union all
select '101','16:00','8/16/2010' union all
select '101','Off','8/17/2010' union all
select '101','17:00','8/18/2010' union all
select '101','17:00','8/19/2010' union all
select '101','16:00','8/20/2010' union all
select '101','16:00','8/21/2010' union all
select '101','16:00','8/22/2010' union all
select '101','16:00','8/23/2010' union all
select '101','16:00','8/24/2010' union all
select '101','Off','8/25/2010' union all
select '101','17:00','8/26/2010' union all
select '101','17:00','8/27/2010' union all
select '101','16:00','8/28/2010' union all
select '102','Off','8/1/2010' union all
select '102','17:00','8/2/2010' union all
select '102','17:00','8/3/2010' union all
select '102','16:00','8/4/2010' union all
select '102','16:00','8/5/2010' union all
select '102','16:00','8/6/2010' union all
select '102','16:00','8/7/2010' union all
select '102','16:00','8/8/2010' union all
select '102','Off','8/9/2010' union all
select '102','17:00','8/10/2010' union all
select '102','17:00','8/11/2010' union all
select '102','16:00','8/12/2010' union all
select '102','16:00','8/13/2010' union all
select '102','16:00','8/14/2010' union all
select '102','16:00','8/15/2010' union all
select '102','16:00','8/16/2010' union all
select '102','Off','8/17/2010' union all
select '102','17:00','8/18/2010' union all
select '102','17:00','8/19/2010' union all
select '102','16:00','8/20/2010' union all
select '102','16:00','8/21/2010' union all
select '102','16:00','8/22/2010' union all
select '102','16:00','8/23/2010' union all
select '102','16:00','8/24/2010' union all
select '102','Off','8/25/2010' union all
select '102','17:00','8/26/2010' union all
select '102','21:00','8/27/2010' union all
select '102','16:00','8/28/2010'


ALTER procedure [dbo].[SAR_Sp_DeltaFinal](@WeekStartDate datetime,@CampaignID int)
as
Begin
Declare @TotalEmp int
Declare @EndDate datetime
Declare @StartDate datetime
set @StartDate=dateadd(day,-7,dateadd(day,-7,dateadd(day,-7,@WeekStartDate)))
set @EndDate=dateadd(day,6,@WeekStartDate)
Declare @Across4weekFinal table (empno int,DeltaAcross4Week decimal(3,2))
Declare @Across3weekFinal table (empno int,DeltaAcross3Week decimal(3,2))
Declare @Across2weekFinal table (empno int,DeltaAcross2Week decimal(3,2))
insert into @Across4weekFinal
Exec SAR_Sp_DeltaAcross4week @WeekStartDate,1
insert into @Across3weekFinal
Exec SAR_Sp_DeltaAcross3week @WeekStartDate,@CampaignID
insert into @Across2weekFinal
Exec SAR_Sp_DeltaAcross2week @WeekStartDate,@CampaignID
Select @TotalEmp=2
Select 'With 2 Hrs' as [Delta in Start Time],convert(varchar (10),dbo.FDiv((Select count(empno) from @Across4weekFinal where DeltaAcross4Week<=2.0) ,@TotalEmp)*100)+' %' as [Across Last 4 Weeks],
convert(varchar (10),dbo.FDiv((Select count(empno) from @Across3weekFinal where DeltaAcross3Week<=2.0) ,@TotalEmp)*100)+' %' as [Across Last 3 Weeks],
convert(varchar (10),dbo.FDiv((Select count(empno) from @Across2weekFinal where DeltaAcross2Week<=2.0),@TotalEmp)*100)+' %' as [Across Last 2 Weeks],
convert(varchar,dbo.FDiv(Count(Case When Delta<='0' Then EmpNo end),@TotalEmp)*100)+'%' as 'First Week' FROM SAR_WeekData where WeekStartDate='8/1/2010',
convert(varchar,dbo.FDiv(Count(Case When Delta>='0' Then EmpNo end),@TotalEmp)*100)+'%' as 'Second Week' FROM SAR_WeekData where WeekStartDate>='8/8/2010'
End

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-09 : 11:39:55
Your last SELECT has multiple FROM and WHERE.
That's not possible.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-10 : 10:18:06
sounds like what you need is a pivot on date values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -