2011-07-22 40 views
1

我有一個表本身更新SQL表來自不同行的值

有5列: -

  • 國家代碼
  • 語言
  • 倉庫
  • ActiveFrom
  • ActiveTo

數據的子集(道歉列標題是失準: -

Country 
Code 
    Language 
     Warehouse 
        ActiveFrom   ActiveTo 
AT de BHU 2011-08-08 00:00:00.000 9999-12-31 23:59:59.000 
AT de WGN 2011-02-14 00:00:00.000 9999-12-31 23:59:59.000 
BE fr BHU 2011-09-01 00:00:00.000 9999-12-31 23:59:59.000 
BE fr WGN 2011-02-14 00:00:00.000 9999-12-31 23:59:59.000 
CH de WGN 2011-02-14 00:00:00.000 9999-12-31 23:59:59.000 
CZ sk BHU 2011-08-08 00:00:00.000 9999-12-31 23:59:59.000 
CZ sk WGN 2011-02-14 00:00:00.000 9999-12-31 23:59:59.000 
DE de BHU 2011-09-01 00:00:00.000 9999-12-31 23:59:59.000 
DE de WGN 2011-02-14 00:00:00.000 9999-12-31 23:59:59.000 

我想做的事情,就是更新ActiveTo列,到ActiveFrom日期的值(給予或採取一些毫秒)。

我已經試過這: -

update Translations 
set ActiveTo = DateAdd(ms, -3, ot.ActiveFrom) 
    from Translations ot 
    Where Warehouse = 'WGN' 
    and CountryCode = ot.CountryCode 

,但是,它給了這些結果。

CountryCode Language Warehouse ActiveFrom ActiveTo 
AT de BHU 2011-08-08 00:00:00.000 9999-12-31 23:59:59.000 
AT de WGN 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 
BE fr BHU 2011-09-01 00:00:00.000 9999-12-31 23:59:59.000 
BE fr WGN 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 
CH de WGN 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 
CZ sk BHU 2011-08-08 00:00:00.000 9999-12-31 23:59:59.000 
CZ sk WGN 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 
DE de BHU 2011-09-01 00:00:00.000 9999-12-31 23:59:59.000 
DE de WGN 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 

它正在更新正確的行。但是價值是錯誤的,它從它自己的ActiveFrom日期減去3毫秒,而不是其他倉庫開始日期。

這SQL給出一個派生表正確的結果: -

select t.CountryCode, t.Warehouse, DateAdd (ms, -3, ot.ActiveFrom) as 'TransferToBHU', t.ActiveFrom, t.ActiveTo, 
    ot.CountryCode, ot.Warehouse, ot.ActiveFrom, ot.ActiveTo 
from Translations t 
inner join Translations ot 
    on ot.CountryCode= t.CountryCode 

其中t.Warehouse = 'WGN' 和ot.Warehouse = 'BHU' 訂購t.CountryCode

CountryCode Warehouse TransferToBHU ActiveFrom ActiveTo CountryCode Warehouse ActiveFrom ActiveTo 
AT WGN 2011-08-07 23:59:59.997 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 AT BHU 2011-08-08 00:00:00.000 9999-12-31 23:59:59.000 
BE WGN 2011-08-31 23:59:59.997 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 BE BHU 2011-09-01 00:00:00.000 9999-12-31 23:59:59.000 
CZ WGN 2011-08-07 23:59:59.997 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 CZ BHU 2011-08-08 00:00:00.000 9999-12-31 23:59:59.000 
DE WGN 2011-08-31 23:59:59.997 2011-02-14 00:00:00.000 2011-02-13 23:59:59.997 DE BHU 2011-09-01 00:00:00.000 9999-12-31 23:59:59.000 

現在,如果我可以將'TransferToBHU'中的計算值更新到更新值中,那正是我想要的。

最簡單的解決方案將根據'BHU'值更新'WGN'倉庫值。但是,最好的方法是基於獲取給定CountryCode的兩個最新ActiveFrom日期進行更新。

任何一種解決方案都是可以接受的,但是如果有人提供一個在日期上工作的解決方案 - 他們會得到接受的答案。

在此先感謝。

回答

2
update t1 
set ActiveTo = DateAdd(ms, -3, t2.ActiveFrom) 
from Translations t1 
    JOIN 
    Translations t2 ON t1.CountryCode = t2.CountryCode 
Where t1.Warehouse = 'WGN' AND t2.Warehouse = 'BHU' 

當然,國家CH沒有BHU行,所以:

update t1 
set ActiveTo = DateAdd(ms, -3, ISNULL(t2.ActiveFrom,t1.ActiveFrom)) 
from Translations t1 
    LEFT JOIN 
    Translations t2 ON t1.CountryCode = t2.CountryCode 
Where t1.Warehouse = 'WGN' AND t2.Warehouse = 'BHU' 
+0

什麼可能我做使回答這個問題更容易嗎? – cometbill

+0

@cometbill:不是很多。你提供了樣本數據,期望的結果,你已經嘗試過的。 IMO的好問題。 – gbn

+0

腳本如何創建表格並將示例數據倒入其中?這會幫助其他人嗎? – cometbill