Question

HISTORY: A system has passed through 1 - 19 or so statuses before being moved to production. I need to build a report showing the date the system passed through a status and NA if the system did not pass through a status.

REQUIREMENTS: The report needs to look something like this:

System      Initial     Operations  PIM_Assigned    PIM_Complete    Database    Application 
Server001   9/1/2011    NA          9/2/2011        NA              NA          9/1/2011
Server002   9/10/2011   NA          9/5/2011        9/25/2011       NA          9/9/2011
Server003   9/21/2011   9/22/2011   NA              NA              9/24/2011   NA
Server004   9/23/2011   9/19/2011   9/23/2011       9/20/2011       9/23/2011   9/1/2011

Here is the query with a sample data dump following (dump does not match above - the above is for illustration purposes):

select status, convert(varchar,effectivedate,101) e, systemname  
from si_statushistory
where systemname='SERVER052'  
order by e desc, history_id desc

with output from my query looking like this:

PSI            09/09/2011   SERVER052  
Application    09/09/2011   SERVER052  
Operations     09/09/2011   SERVER052  
Application    07/14/2011   SERVER052  
Operations     07/13/2011   SERVER052  
Operations     07/13/2011   SERVER052  
PSI            07/13/2011   SERVER052  
PIM Assigned   06/08/2011   SERVER052  
PSI            06/08/2011   SERVER052  
SD_Verify      01/15/2012   SERVER052  
PSI Operations 01/08/2012   SERVER052  
Frame Team     01/01/2011   SERVER052

Example of what ONE row would look like:

something is missing here

I hope this is clear and makes sense...

The page is being displayed using Coldfusion and I'm adequate with using Arrays and Structures if that makes this easier to build out. Time of of the essence which is why I'm reaching out for some help. I could do this but I need it sooner than later.

Was it helpful?

Solution

CREATE PROCEDURE dbo.ReturnPivotedSystemInfo
AS
BEGIN
    SET NOCOUNT ON;

    ;WITH x AS
    (
        SELECT 
            [system] = systemname, 
            [status], 
            ed = CONVERT(CHAR(10), effectivedate, 101), -- not varchar w/o length
            rn = ROW_NUMBER() OVER 
                (PARTITION BY systemname, status ORDER BY effectivedate DESC)
        FROM dbo.si_statushistory
        -- where clause here
    )
    SELECT [system], 
        Initial      = COALESCE(MAX(CASE WHEN [status] = 'Initial'      THEN ed END), 'NA'),
        Operations   = COALESCE(MAX(CASE WHEN [status] = 'Operations'   THEN ed END), 'NA'),
        PIM_Assigned = COALESCE(MAX(CASE WHEN [status] = 'PIM Assigned' THEN ed END), 'NA')
        --, repeat for other possible values of status
    FROM x
    WHERE rn = 1
    GROUP BY [system];
END
GO

Now your ColdFusion just needs to execute the stored procedure dbo.ReturnPivotedSystemInfo and from there on it should be able to behave just as if you had called SELECT * FROM sometable...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top