如何填写以前的记录

时间:2016-04-23 23:46:33

标签: sql

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

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

想用上一步填写空记录。 填写上面的值后,我发现Exception Start和Exception End想要填写上面的值。 请参考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                 |
+---------+------------------------+----------------------------+----------------------------+----------------------------+

Input

Output

1 个答案:

答案 0 :(得分:1)

不同的数据库有不同的方法来解决这个问题。一种方法在select中使用相关子查询。典型的方法可以用ANSI标准SQL表示为:

update t
     set step2 = (select t2.processstep
                  from t t2
                  where t2.no = t.no and
                        t2.date <= t.date and
                        t2.processstep is not null
                  order by date desc
                  fetch first 1 row only
                 );

在某些数据库中,您可以使用limittop来获取第一行。即便如此,这可能不适用于所有数据库,但类似的东西可能会起作用。