2016-04-21 52 views
0

我有一个表,即无,日期和步骤如何填写以前的记录和基于条件?

+---------+----------------------------+----------------------------+ 
| No |      Date |   Step   | 
+---------+----------------------------+----------------------------+ 
| 643995G | 03/12/2012 3:22:48 PM  | TransferInStart   | 
| 643995G | 03/12/2012 3:22:50 PM  |       | 
| 643995G | 15/02/2013 10:53:57 AM  | Hold      | 
| 643995G | 15/02/2013 10:54:00 AM  | Hold Copy Processing Start | 
| 643995G | 20/02/2013 4:38:26 PM  |       | 
| 643995G | 21/02/2013 3:27:01 PM  | ExceptionStart    | 
| 643995G | 22/02/2013 9:38:32 AM  | ExceptionEnd    | 
| 643995G | 22/02/2013 9:39:32 AM  |       | 
| 643995G | 22/02/2013 10:04:53 AM  |       | 
| 643995G | 22/02/2013 10:04:56 AM  |       | 
| 643995G | 25/02/2013 10:48:18 AM  | TransferIn     | 
| 643995G | 25/02/2013 10:48:19 AM  | CatalougingStart   | 
| 643995G | 27/02/2013 1:59:44 PM  |       | 
| 643995G | 27/02/2013 1:59:45 PM  |       | 
| 643995G | 27/02/2013 2:05:01 PM  | Catalouging    | 
| 643995G | 27/02/2013 2:05:02 PM  | ProcessingStart   | 
| 643995G | 27/02/2013 6:09:51 PM  |       | 
| 643995G | 27/02/2013 6:09:52 PM  |       | 
| 643995G | 04/03/2013 2:03:16 PM  |       | 
| 643995G | 04/03/2013 2:03:17 PM  | Hold Copy Processing Start | 
| 643995G | 11/03/2013 2:27:14 PM  |       | 
| 643995G | 12/03/2013 10:09:55 AM  | ExceptionStart    | 
| 643995G | 12/03/2013 10:10:27 AM  | ExceptionEnd    | 
| 643995G | 12/03/2013 10:10:52 AM  |       | 
| 643995G | 12/03/2013 10:11:27 AM  |       | 
| 643995G | 12/03/2013 10:28:37 AM  |       | 
| 643995G | 12/03/2013 10:46:21 AM  | Processing     | 
+---------+----------------------------+----------------------------+ 
  1. 以下数据想填补与上一步中的空记录。

  2. 在填写上述值之后,我发现异常开始和异常结束想填写上述值。

请参考吹步骤2和步骤3作为输出示例。

+---------+------------------------+----------------------------+----------------------------+----------------------------+ 
| No |   Date   |  ProcessStep   |   Step2   |   Step3   | 
+---------+------------------------+----------------------------+----------------------------+----------------------------+ 
| 643995G | 03/12/2012 3:22:48 PM | TransferInStart   | TransferInStart   | TransferInStart   | 
| 643995G | 03/12/2012 3:22:50 PM |       | TransferInStart   | TransferInStart   | 
| 643995G | 15/02/2013 10:53:57 AM | Hold      | Hold      | Hold      | 
| 643995G | 15/02/2013 10:54:00 AM | Hold Copy Processing Start | Hold Copy Processing Start | Hold Copy Processing Start | 
| 643995G | 20/02/2013 4:38:26 PM |       | Hold Copy Processing Start | Hold Copy Processing Start | 
| 643995G | 21/02/2013 3:27:01 PM | ExceptionStart    | ExceptionStart    | Hold Copy Processing Start | 
| 643995G | 22/02/2013 9:38:32 AM | ExceptionEnd    | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 22/02/2013 9:39:32 AM |       | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 22/02/2013 10:04:53 AM |       | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 22/02/2013 10:04:56 AM |       | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 25/02/2013 10:48:18 AM | TransferIn     | TransferIn     | TransferIn     | 
| 643995G | 25/02/2013 10:48:19 AM | CatalougingStart   | CatalougingStart   | CatalougingStart   | 
| 643995G | 27/02/2013 1:59:44 PM |       | CatalougingStart   | CatalougingStart   | 
| 643995G | 27/02/2013 1:59:45 PM |       | CatalougingStart   | CatalougingStart   | 
| 643995G | 27/02/2013 2:05:01 PM | Catalouging    | Catalouging    | Catalouging    | 
| 643995G | 27/02/2013 2:05:02 PM | ProcessingStart   | ProcessingStart   | ProcessingStart   | 
| 643995G | 27/02/2013 6:09:51 PM |       | ProcessingStart   | ProcessingStart   | 
| 643995G | 27/02/2013 6:09:52 PM |       | ProcessingStart   | ProcessingStart   | 
| 643995G | 04/03/2013 2:03:16 PM |       | ProcessingStart   | ProcessingStart   | 
| 643995G | 04/03/2013 2:03:17 PM | Hold Copy Processing Start | Hold Copy Processing Start | Hold Copy Processing Start | 
| 643995G | 11/03/2013 2:27:14 PM |       | Hold Copy Processing Start | Hold Copy Processing Start | 
| 643995G | 12/03/2013 10:09:55 AM | ExceptionStart    | ExceptionStart    | Hold Copy Processing Start | 
| 643995G | 12/03/2013 10:10:27 AM | ExceptionEnd    | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 12/03/2013 10:10:52 AM |       | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 12/03/2013 10:11:27 AM |       | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 12/03/2013 10:28:37 AM |       | ExceptionEnd    | Hold Copy Processing Start | 
| 643995G | 12/03/2013 10:46:21 AM | Processing     | Processing     | Processing     | 
+---------+------------------------+----------------------------+----------------------------+----------------------------+ 

连接输入表的图像清晰

连接输出值的图像清晰

+0

您使用的是什么版本的sql-server? – mxix

+0

@kitees,如果您的SO将会被激活,请使用这个收费来格式化您的示例数据:http://www.sensefulsolutions.com/2010/10/format-text-as-table.html – FLICKER

+0

Hi Mxix ..我正在使用版本2008 R2。 – kitees

回答

0

随着APPLY你可以做这些“查找”。无法验证所有语法是否正确,但应指出可能的解决方案。

with myTable as (
    select 
     [No], 
     CONVERT(DATETIME,[Date],131)[Date], 
     [Step] 
    from (
    values 
    ('643995G','03/12/2012 3:22:48 PM','TransferInStart'), 
    ('643995G','03/12/2012 3:22:50 PM',NULL), 
    ('643995G','15/02/2013 10:53:57 AM','Hold'), 
    ('643995G','15/02/2013 10:54:00 AM','Hold Copy Processing Start'), 
    ('643995G','20/02/2013 4:38:26 PM',NULL), 
    ('643995G','21/02/2013 3:27:01 PM','ExceptionStart'), 
    ('643995G','22/02/2013 9:38:32 AM','ExceptionEnd'), 
    ('643995G','22/02/2013 9:39:32 AM',NULL), 
    ('643995G','22/02/2013 10:04:53 AM',NULL), 
    ('643995G','22/02/2013 10:04:56 AM',NULL), 
    ('643995G','25/02/2013 10:48:18 AM','TransferIn'), 
    ('643995G','25/02/2013 10:48:19 AM','CatalougingStart'), 
    ('643995G','27/02/2013 1:59:44 PM',NULL), 
    ('643995G','27/02/2013 1:59:45 PM',NULL), 
    ('643995G','27/02/2013 2:05:01 PM','Catalouging'), 
    ('643995G','27/02/2013 2:05:02 PM','ProcessingStart'), 
    ('643995G','27/02/2013 6:09:51 PM',NULL), 
    ('643995G','27/02/2013 6:09:52 PM',NULL), 
    ('643995G','04/03/2013 2:03:16 PM',NULL), 
    ('643995G','04/03/2013 2:03:17 PM','Hold Copy Processing Start'), 
    ('643995G','11/03/2013 2:27:14 PM',NULL), 
    ('643995G','12/03/2013 10:09:55 AM','ExceptionStart'), 
    ('643995G','12/03/2013 10:10:27 AM','ExceptionEnd'), 
    ('643995G','12/03/2013 10:10:52 AM',NULL), 
    ('643995G','12/03/2013 10:11:27 AM',NULL), 
    ('643995G','12/03/2013 10:28:37 AM',NULL), 
    ('643995G','12/03/2013 10:46:21 AM','Processing') 
    ) t ([No],[Date],[Step]) 
) 
select 
    [No], 
    [Date], 
    mT.[Step] [ProcessStep], 
    CASE WHEN mT.[Step] is null then e.[Step] else mT.[Step] end [Step2], 
    case when mT.[Step] in ('ExceptionStart','ExceptionEnd') OR e.Step in ('ExceptionStart','ExceptionEnd') then iHateStartAndEnd.Step else COALESCE(mt.[Step],e.[Step]) end [Step3] 
from myTable mT 
outer apply (
    select top 1 
     eValue.[Step] 
    from myTable eValue 
    where 
     mT.[No] = eValue.[No] and 
     mt.[Date] > eValue.[Date] and 
     isnull(eValue.[Step],'') <> '' 
    order by 
     eValue.[Date] desc 
) e 
outer apply (
    select top 1 
     eValue2.[Step] 
    from myTable eValue2 
    where 
     mT.[No] = eValue2.[No] and 
     mT.[Date] >= eValue2.[Date] and 
     eValue2.[Step] not in ('ExceptionStart','ExceptionEnd') and 
     eValue2.[Step] is not null 
    order by 
     eValue2.[Date] desc 
) iHateStartAndEnd 
+0

嗨Mxix,谢谢您的回复。我只有29条记录,但是当我试图执行你的查询时,它给出了1667行的输出。请求您的帮助来检查此.. – kitees

+0

@kitees完整的工作查询与示例数据。 – mxix

+0

你能帮我解决这个问题吗... – kitees