I am currently creating a gridview that has dynamic columns.
I have successfully created this from querying the log hours for every task per date on a specific sprint. The logged dates becomes the column and the task and logged hours become rows. With both vertical and horizontal totals. On this gridview, you can manually edit the logged hours and it will be saved to the database.
The problem now is the design changes. What I need to do is just list the sprint duration dates for columns and task on the first column even if there are still no logged hour on that task or date.
Any help will be appreciated. Thanks.
here is the stored procedure for this pivoted table
USE [JiraAutomation]
GO
/****** Object: StoredProcedure [dbo].[logs] Script Date: 4/12/2016 7:00:09 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[logs]
@username nvarchar(30),
@sprintId nvarchar(30)
AS
/* COLUMNS HEADERS */
Declare
@cols as NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(log_date)
from tbl_log join tbl_task on tbl_task.task_id = tbl_log.task_id
where tbl_log.username = @username
and tbl_log.sprint_id = @sprintId
group by log_date
order by log_date
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
DECLARE @horiz_total nvarchar(MAX)
SELECT @horiz_total = stuff((
SELECT '+isnull(' + quotename(log_date) + ',0)'
FROM tbl_log
join tbl_task on tbl_task.task_id = tbl_log.task_id
where tbl_log.username = @username
and tbl_log.sprint_id = @sprintId
GROUP BY log_date
ORDER BY log_date
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')
DECLARE @vert_total nvarchar(MAX)
SELECT @vert_total = stuff((
SELECT ',sum(' + quotename(log_date) + ')'
FROM tbl_log
join tbl_task on tbl_task.task_id = tbl_log.task_id
where tbl_log.username = @username
and tbl_log.sprint_id = @sprintId
GROUP BY log_date
ORDER BY log_date
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')
DECLARE @isnulls nvarchar(MAX)
SELECT @isnulls = stuff((
SELECT ',isnull(' + quotename(log_date) + ',0) as '+quotename(log_date)
FROM tbl_log
GROUP BY log_date
ORDER BY log_date
FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')
DECLARE @query nvarchar(MAX)
SET @query = 'select task_description as TASK,' + @cols + ',' + @horiz_total + ' as Total
into #tmp_result
from (select task_description, log_date, log_hours from tbl_log join tbl_task on tbl_task.task_id = tbl_log.task_id
where tbl_log.username = '''+@username+'''
and tbl_log.sprint_id = '''+@sprintId+'''
) x
pivot (sum(log_hours) for log_date in (' + @cols + ')) p
select *
from #tmp_result
union all
SELECT '''','+@vert_total +',
ISNULL (SUM([Total]),0) FROM #tmp_result
DROP TABLE #tmp_result'
-- PRINT 'Pivot Query '+@FinalQuery
EXECUTE(@query)