1

よろしくお願いします。

参照テーブルと詳細テーブルの 2 つのテーブルがあります。参照テーブルには、次のステップとペアになったオーダーの現在の生産ステップがリストされています。

参照表

Current_Step      | Next_Step          |  ID
-------------------------------------------------
Step 1            | Step 2             | 1
Step 2            | Step 3             | 2
Step 3            | Step 4             | 3

注文詳細テーブルもあります。

Order_ID  | Step_ID  | Start_Date | Planned_End | Complete_Date | Planned_Duration
-----------------------------------------------------------------------------------
1000      | 1        | 1/1/2013   | 1/3/2013    | 1/3/2013      | 2
1000      | 2        |            |             |               | 3
1000      | 3        |            |             |               | 8

このテーブルには、注文の各ステップが存在しますが、開始日と終了予定日が空白になっています。

次のクエリを作成しようとしています。

  • 今日の完全な日付を持つすべてのアイテムを検索します
  • テーブル内の同じ Order_ID のアイテムに関連付けられている Next_Step を見つけます
  • Start_Date が空白の場合、Start_Date を今日に更新し、Planned_Duration の日数を開始日に追加して、Planned_End の日付を計算します。

一部を個別に実行することはできますが、すべてを単一のクエリ/ストアド プロシージャにまとめるのに苦労しています。

正しい方向への指針をいただければ幸いです。

再度、感謝します!

4

3 に答える 3

0
declare @Today datetime

select @Today = dateadd(dd, datediff(dd, 0, getdate()), 0)

update od2 set
    start_date = @Today,
    planned_end = dateadd(dd, od2.planned_duration, @Today)
from order_details as od
    inner join reference_table as rt on rt.current_step_id = od.step_id
    inner join order_details as od2 on od2.order_id = od.id and od2.step_id = rt.next_step_id
where
    od.complete_date = @Today and od2.start_date is null
于 2013-09-23T20:22:37.997 に答える
0

私は次のようなことから始めます:

 select 
    n.orderId
 from
    refernce_table as r
    inner join order_details as p
        on r.current_step = p.step_id
    inner join order_details as n
        on r.next_step = n.step_id
 where 
     p.complete_date is not null
     and n.start_date is null

これにより、前のステップが p として表示され、次のステップが n として表示されます。必要に応じてそれらをフィルタリングします。

ここでは、前のステップが完了し、次のステップが開始されていないという命令を私にくださいと言っています。おもう ;)

于 2013-09-23T20:17:01.937 に答える