Select Team,
isNull([1006], 0)+isNull([1007], 0)+isNull([1008], 0)+isNull([1009], 0)+isNull([1010], 0) as SP1005,
isNull([1007], 0)+isNull([1008], 0)+isNull([1009], 0)+isNull([1010], 0) as SP1006,
isNull([1008], 0)+isNull([1009], 0)+isNull([1010], 0) as SP1007,
isNull([1009], 0)+isNull([1010], 0) as SP1008,
isNull([1010], 0) as SP1009,
0 as SP1010
From YourTable
PIVOT (Sum(WorkHours) For Sprint in ([1005],[1006],[1007],[1008],[1009],[1010])) p
The problem is if you want this to be dynamic. In that case you should use dynamic sql query. Construction of dynamic query depends on several things:
- Do you want variable number of
columns or not
Do you want to pass:
a) first month / last month or
b) list of months (eg. comma separated)
c) just start month and query should return columns up to the current month
d) just start month and query should return columns up to the last month you have in table
UPDATED
Version for SQL SERVER 2000
Select Team,
sum(SP1005) as SP1005,
sum(SP1006) as SP1006,
sum(SP1007) as SP1007,
sum(SP1008) as SP1008,
sum(SP1009) as SP1009,
0 as SP1010
From (
Select Team,
Case when Sprint in (1006, 1007, 1008, 1009, 1010) Then WorkHours Else 0 end as SP1005,
Case when Sprint in (1007, 1008, 1009, 1010) Then WorkHours Else 0 end as SP1006,
Case when Sprint in (1008, 1009, 1010) Then WorkHours Else 0 end as SP1007,
Case when Sprint in (1009, 1010) Then WorkHours Else 0 end as SP1008,
Case when Sprint in (1010) Then WorkHours Else 0 end as SP1009
From @t
) a
Group by Team
UPDATE 2: ADDED DYNAMIC STORED PROCEDURE
(For SQL Server 2000+)
I assumed your Sprint column is integer.
Alter Procedure zzzGetTeamMonths( @FirstMonth int )
AS BEGIN
Declare @FirstMonthDate datetime
Declare @ActFirstMonth int
Declare @LastMonth int
Declare @LastMonthChar nvarchar(4)
Declare @nMonths int
Select @LastMonth=max(Sprint),
@ActFirstMonth=min(Sprint)
from zzzTest
Where Sprint >= @FirstMonth
If @LastMonth >= @FirstMonth Begin
-- Next line remove repeating columns from left side (for non-existant data)
-- You can comment it out if you want to see all columns
IF @ActFirstMonth>@FirstMonth Set @FirstMonth=@ActFirstMonth
Set @nMonths = (@LastMonth/100-@FirstMonth/100)*12 + (@LastMonth-(@LastMonth/100)*100-@FirstMonth+(@FirstMonth/100)*100)
Set @FirstMonthDate = convert(datetime, Right('0'+cast(@FirstMonth as varchar(4)),4)+'01',12)
Set @LastMonthChar = Right('0'+cast(@LastMonth as varchar(4)),4)
Create Table #Months (id int, mnt nvarchar(4), mntp nvarchar(4))
Insert Into #Months
Select Number,
Convert(nvarchar(4),Dateadd(month,number,@FirstMonthDate),12),
Convert(nvarchar(4),Dateadd(month,number-1,@FirstMonthDate),12)
From master.dbo.spt_values
Where type='P' and number<=@nMonths
Declare @cmd nvarchar(4000)
Declare @tmp nvarchar(4000)
Declare @col nvarchar(4000)
Select @tmp = '',
@col = ''
Select @col = @col + '
,sum(SP' + mntp + ') as SP' + mntp,
@tmp = @tmp + '
,Case when Sprint between ' + mnt + ' and ' + @LastMonthChar + ' Then WorkHours Else 0 end as SP' + mntp
from #Months
Drop Table #Months
Select @cmd = '
SELECT Team' + @col + '
,0 as SP' + @LastMonthChar + '
FROM
(
Select Team' + @tmp + '
From zzzTest
) a
Group by Team'
Print @cmd
Exec (@cmd)
End
RETURN 0
END
GO