HISTORY:一个系统在被转移到生产之前已经通过了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
以下是带有示例数据转储的查询(转储与上面的不匹配-上面的内容用于说明(:
选择状态,转换(varchar,生效日期,101(e,系统名称来自si-statushistory其中系统名称="SERVER052"按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显示的,如果使用Arrays和Structures可以更容易地构建,那么我就足够了。时间至关重要,这就是我寻求帮助的原因。我可以这样做,但我迟早需要它。
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
一样工作。。。