Question

I have a table that looks like this.

Dept   |           TicketCode                 |  AsOfDate  |FixedBy | FixedDate
--------------------------------------------------------------------------------
merlin | ACE4E957-62C9-4447-A39E-AAA6928C7DD3 | 2014-03-04 | Mark   | 2014-03-05 
merlin | 95C5AF27-3211-E211-8E73-002481E28F48 | 2014-02-27 | Mark   | 2014-03-06
merlin | 581CE204-3586-E211-A244-002481E28F48 | 2014-02-27 | Ravi   | 2014-03-06
merlin | E9E6C0C6-7562-4265-82B0-5D14E3FEC674 | 2014-03-17 | Olive  | 2014-03-18
omega  | 1922DD26-3211-E211-8E73-002481E28F48 | 2014-03-18 | Sandy  | 2014-03-19
merlin | 94E6EF27-3211-E211-8E73-002481E28F48 | 2014-03-18 | Ravi   | 2014-03-19
omega  | E7F5EF27-3211-E211-8E73-002481E28F48 | 2014-03-18 | Sandy  | 2014-03-19
omega  | CF8D4227-3211-E211-8E73-002481E28F48 | 2014-03-19 | Olive  | 2014-03-20
omega  | 1A904227-3211-E211-8E73-002481E28F48 | 2014-03-19 | Ravi   | 2014-03-20
merlin | DCA94227-3211-E211-8E73-002481E28F48 | 2014-03-19 | Steve  | 2014-03-20
Pine   | 349E868F-DFFB-43DC-B50E-A9FFBF553908 | 2014-03-19 | Steve  | 2014-03-20
merlin | 281FDD26-3211-E211-8E73-002481E28F48 | 2014-03-20 | Olive  | 2014-03-21
omega  | FDB6AF27-3211-E211-8E73-002481E28F48 | 2014-03-20 | Steve  | 2014-03-21
omega  | B8A1A320-FD0E-45AD-8A5D-9E92529806CD | 2014-04-29 | Ravi   | 2014-04-30
omega  | B8A1A320-FD0E-45AD-8A5D-9E92529806CD | 2014-04-29 | Olive  | 2014-04-30
Pine   | 5369D01E-BA2E-4AA6-A228-A9073BC0AE1B | 2014-04-29 | Steve  | 2014-04-30
Pine   | 1B59E55F-4AFB-490C-901C-82E0743748C6 | 2014-04-30 | Sandy  | 2014-05-01
Pine   | B326348F-E838-42FD-BECB-A8071175BC27 | 2014-04-30 | Ravi   | 2014-05-01
Merlin | B326348F-E838-42FD-BECB-A8071175BC27 | 2014-04-30 | Sandy  | 2014-05-01

now my requirement is in such a way that the table which i am supposed to display should be having variable columns based on the given date range, and also i am supposed to count the "TicketCode" with filter of dept, FixedBy, FixedDate.

To Put it Simple, This is what i am supposed to get.

--Given dates range

@start_date = '2014-03-05'
@end_date = '2014-03-08'

Dept   | FixedBy | 2014-03-05 | 2014-03-06 | 2014-03-07  | 2014-03-08 | 
-----------------------------------------------------------------------
merlin |   Mark  |     20     |     111    |     24      |     853    |
merlin |   Ravi  |     26     |     456    |     289     |     463    |
merlin |   Mark  |     85     |     81     |     24      |     801    |
merlin |   Steve |     75     |     0      |     0       |     1157   |
merlin |   Sandy |     0      |     78     |     24      |     789    |
merlin |   Mark  |     166    |     110    |     0       |     176    |
omega  |   Ravi  |     126    |     7456   |     289     |     63     |
omega  |   Mark  |     885    |     81     |     284     |     01     |
omega  |   Steve |     975    |     0      |     0       |     157    |
omega  |   Sandy |     90     |     78     |     24      |     79     |
omega  |   Mark  |     166    |     10     |     0       |     76     |
Pine   |   Sandy |     0      |     78     |     24      |     789    |
Pine   |   Mark  |     166    |     10     |     0       |     106    |
Pine   |   Ravi  |     126    |     746    |     289     |     63     |

NOTE: Count is base on Ticket Code, Date range is not fixed. Code can contain #temp tables

Mark might work for all the depts, but we only want the dept statics for the given dates and contributed progress.

I have tried every available resources i can find. I just couldn't pass through.

FYI: Date range is given manually, so column's count differs. Code can contain #temp tables but no creation functions (i am not Admin).

Many many Thanks in advance.

Was it helpful?

Solution

Here is some code for MS-SQL that will give the results you are looking for. It creates a dynamics sSQL script and executes this to get the results. If this does not solve your problem 100% I hope it at least points you in the correct direction.

CREATE TABLE  #Data (Dept VARCHAR(20),TicketCode VARCHAR(100), AsOfDate DATETIME, FixedBy VARCHAR(10), FixedDate DATETIME)

DECLARE @StartDate      AS  DATETIME
DECLARE @EndDate        AS  DATETIME
DECLARE @ProcessDate    AS  DATETIME
DECLARE @sSQL           AS  VARCHAR(MAX)
DECLARE @dtString       AS  VARCHAR(100)

SET @StartDate  = '2014-03-19'
SET @EndDate    = '2014-03-21'

INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' ACE4E957-62C9-4447-A39E-AAA6928C7DD3 ',' 2014-03-04 ',' Mark   ',' 2014-03-05 ')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' 95C5AF27-3211-E211-8E73-002481E28F48 ',' 2014-02-27 ',' Mark   ',' 2014-03-06')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' 581CE204-3586-E211-A244-002481E28F48 ',' 2014-02-27 ',' Ravi   ',' 2014-03-06')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' E9E6C0C6-7562-4265-82B0-5D14E3FEC674 ',' 2014-03-17 ',' Olive  ',' 2014-03-18')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' 1922DD26-3211-E211-8E73-002481E28F48 ',' 2014-03-18 ',' Sandy  ',' 2014-03-19')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' 94E6EF27-3211-E211-8E73-002481E28F48 ',' 2014-03-18 ',' Ravi   ',' 2014-03-19')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' E7F5EF27-3211-E211-8E73-002481E28F48 ',' 2014-03-18 ',' Sandy  ',' 2014-03-19')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' CF8D4227-3211-E211-8E73-002481E28F48 ',' 2014-03-19 ',' Olive  ',' 2014-03-20')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' 1A904227-3211-E211-8E73-002481E28F48 ',' 2014-03-19 ',' Sandy   ',' 2014-03-19')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' DCA94227-3211-E211-8E73-002481E28F48 ',' 2014-03-19 ',' Steve  ',' 2014-03-20')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('Pine   ',' 349E868F-DFFB-43DC-B50E-A9FFBF553908 ',' 2014-03-19 ',' Steve  ',' 2014-03-20')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('merlin ',' 281FDD26-3211-E211-8E73-002481E28F48 ',' 2014-03-20 ',' Olive  ',' 2014-03-21')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' FDB6AF27-3211-E211-8E73-002481E28F48 ',' 2014-03-20 ',' Steve  ',' 2014-03-21')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' B8A1A320-FD0E-45AD-8A5D-9E92529806CD ',' 2014-04-29 ',' Ravi   ',' 2014-04-30')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('omega  ',' B8A1A320-FD0E-45AD-8A5D-9E92529806CD ',' 2014-04-29 ',' Olive  ',' 2014-04-30')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('Pine   ',' 5369D01E-BA2E-4AA6-A228-A9073BC0AE1B ',' 2014-04-29 ',' Steve  ',' 2014-04-30')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('Pine   ',' 1B59E55F-4AFB-490C-901C-82E0743748C6 ',' 2014-04-30 ',' Sandy  ',' 2014-05-01')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('Pine   ',' B326348F-E838-42FD-BECB-A8071175BC27 ',' 2014-04-30 ',' Ravi   ',' 2014-05-01')
INSERT INTO #Data (Dept, TicketCode, AsOfDate, FixedBy, FixedDate) VALUES('Merlin ',' B326348F-E838-42FD-BECB-A8071175BC27 ',' 2014-04-30 ',' Sandy  ',' 2014-05-01')

SET @ProcessDate = @StartDate 

SET @sSQL = 'SELECT Dept,FixedBy '

/*Create the Dynamics SQL for the Columns*/
WHILE @ProcessDate < @EndDate
    BEGIN
        SET @dtString = CAST(YEAR(@ProcessDate) AS CHAR(4)) + '_'  +  CAST(MONTH(@ProcessDate) AS VARCHAR(2)) + '_' + CAST(DAY(@ProcessDate) AS VARCHAR(2))
        SET @sSQL = @sSQL + ',
            SUM(CASE WHEN FixedDate = ''' + CAST(@ProcessDate AS CHAR(20)) +  ''' THEN  1 ELSE 0 END) AS dt_' + @dtString
        SET @ProcessDate = @ProcessDate + 1
    END 
SET
    @sSQL = @sSQL +  ' FROM #Data GROUP BY Dept,FixedBy ORDER BY Dept,FixedBy '

SELECT  @sSQL /*To see what will execute*/ 

EXECUTE (@sSQL)  /*Run the Query to return results*/ 

DROP TABLE #Data  /*Remove table*/

OTHER TIPS

It is relatively easy to solve with dynamic pivot. However, your results don't match your data -- there are only two rows in sample data for range between '2014-03-05' and '2014-03-08'.

Here's the code:

declare @start_date as date,
@end_date as date;

select @start_date = '2014-03-05', @end_date = '2014-03-08';

declare @collist nvarchar(max)
set @collist = stuff((select distinct ',' + quotename(convert(varchar(8), FixedDate, 112)) 
    from #t -- your table here
    where FixedDate between @start_date and @end_date
    for xml path(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

declare @q nvarchar(max)
set @q = '
select Dept, FixedBy, ' + @collist + ' 
from (
    select Dept, FixedBy, FixedDate, TicketCode
        from (
        select *
        from #t -- your table here
    where FixedDate between ''' + convert(varchar(8),@start_date,112) + ''' and ''' + convert(varchar(8),@end_date,112) + '''
    ) as x
) as source
pivot (
    count(TicketCode)
    for FixedDate in (' + @collist + ')
) as pvt
'
exec (@q)

Result (using the insert script by @Richard, thx):

Dept                 FixedBy    20140305    20140306
-------------------- ---------- ----------- -----------
merlin                Mark      1           1
merlin                Ravi      0           1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top