2016-04-21 6 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. 上記の値を入力した後で、例外開始と例外終了が上記の値で満たしたいと思った箇所が見つかりました。

出力例として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     | 
+---------+------------------------+----------------------------+----------------------------+----------------------------+ 

入力テーブルのイメージは、あなたがこれらの「ルックアップ」を行うことができますAPPLY

+0

どのバージョンのSQL Serverを使用していますか? – mxix

+0

@kitees、あなたがSO上でアクティブになる場合は、サンプルデータのフォーマットにこの通話料を使用してください:http://www.sensefulsolutions.com/2010/10/format-text-as-table.html – FLICKER

+0

こんにちはMxix私はバージョン2008 R2を使用しています。 – kitees

答えて

0

明確にする出力値の画像を取り付ける

明確にすることが添付。すべての構文が正しいかどうかは検証できませんが、可能な解決策を指摘する必要があります。

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