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

发布于 2024-12-05 17:58:07 字数 1487 浏览 1 评论 0原文

历史记录:系统在投入生产之前已经经历了 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

一行的示例看起来像:

something is missing here

我希望这是清楚且有意义的...

该页面是使用 Coldfusion 显示的,如果这使得它更容易构建,我足以使用数组和结构。关键时刻,这就是我寻求帮助的原因。我可以做到这一点,但我迟早需要它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲性难收 2024-12-12 17:58:07
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...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文