我有一张桌子
有5列: -
数据子集(列标题不对齐的道歉: -
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日期。
任何一种解决方案都是可以接受的,尽管如果有人提供一种适用于日期的解决方案 - 他们会得到接受的答案。
提前致谢。
答案 0 :(得分: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'