Coldfusion:需要帮助在每个系统的一行而不是多行上输出查询

时间:2011-09-22 16:42:03

标签: sql-server-2005 coldfusion-8 cfquery

历史:系统在转移到生产之前已经过了1到19个左右的状态。我需要构建一个报告,显示系统通过状态的日期,如果系统没有通过状态,则需要NA。

要求:报告需要看起来像这样:

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

以下是带有示例数据转储的查询(转储与上面不匹配 - 以上内容仅供参考):

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

我查询的输出如下所示:

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

ONE行的示例:

something is missing here

我希望这很明确并且有道理......

使用Coldfusion显示页面,如果这样可以更容易构建,我就足以使用Arrays和Structures。时间的本质,这就是为什么我要寻求一些帮助。我能做到这一点,但我需要的比较早。

1 个答案:

答案 0 :(得分:0)

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

现在你的ColdFusion只需要执行存储过程dbo.ReturnPivotedSystemInfo,从那里开始就应该能够表现得像调用SELECT * FROM sometable ...