如何填写以前的记录和基于条件的?

时间:2016-04-21 23:17:09

标签: sql-server

我在表格中有以下数据,即“否,日期和步骤”

+---------+----------------------------+----------------------------+
|   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. 填写上述值后,我发现Exception Start和Exception End想填写上面的值。

  3. 请参考blow step2和Step3作为示例输出。

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

    附加输入表的图像以使其清晰

    附加输出值的图像以使其清晰

1 个答案:

答案 0 :(得分: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