0

我有一张表,需要根据即将到来的天气预报每小时更新一次。创建表和合并效果很好,我最近一直在使用它们。

CREATE TABLE #WeatherHourly([Pk_Id][int],
                            [ObservationDateTime] [datetime],    
                            [EffectiveDateTime] [datetime],
                            [Value] [decimal](18, 4)
                           )

MERGE INTO WeatherHourly AS TARGET 
USING #WeatherHourly AS Source ON Target.Pk_Id = Source.Pk_Id 
                               AND Target.EffectiveDateTime = Source.EffectiveDateTime 
WHEN MATCHED THEN
   UPDATE SET Target.Value = Source.Value,
              Target.ObservationDateTime = Source.ObservationDateTime 
WHEN NOT MATCHED BY TARGET THEN 
   INSERT ([Pk_Id], [ObservationDateTime], [EffectiveDateTime], [Value]) 
   VALUES (Source.Pk_Id, Source.ObservationDateTime, Source.EffectiveDateTime, Source.Value);

但我注意到,有时新的预测不包括我已经写入表格的旧数据。这意味着一些数据会留下来,因为没有任何东西在合并语句中覆盖/更新它。该数据实际上不再有效,因为它未包含在最新预测中。现在只需在合并中添加“与源不匹配”即可,尽管它会删除所有历史记录行。由于其他原因,我目前保留历史记录并且不能删除旧行,但只能删除 WeatherHourly 表中仍然相关的行/(EffectiveDateTime 在从昨天到任何未来日期的范围内)。下面的代码是我迄今为止尝试过的,但它会删除我不想删除的所有历史记录行。

Delete from WeatherHourly from WeatherHourly tRow 
left JOIN #WeatherHourly t2 ON tRow.EffectiveDateTime = t2.EffectiveDateTime and   tRow.Pk_Id = t2.Pk_Id
where t2.PK_Id is null

任何帮助,将不胜感激。

为我的新方法编辑:我想知道为我的原始合并使用更好的选择来使目标更符合我的需要。

WITH    t2
AS
(
SELECT  *
FROM    WeatherHourly t
WHERE   EXISTS
(
SELECT * FROM #WeatherHourly r WHERE t.EffectiveDateTime = r.EffectiveDateTime AND  t.Pk_Id = r.Pk_Id
)
)
MERGE t2
AS Target
USING   #WeatherHourly
AS Source
ON Target.Pk_Id = Source.Pk_Id 
    AND Target.EffectiveDateTime = Source.EffectiveDateTime 
WHEN MATCHED THEN 
    UPDATE SET Target.Value = Source.Value,Target.ObservationDateTime = Source.ObservationDateTime 
WHEN NOT MATCHED BY TARGET THEN
    INSERT ([Pk_Id],[ObservationDateTime],[EffectiveDateTime],[Value]) 
    VALUES (SourcePk_Id,Source.ObservationDateTime,Source.EffectiveDateTime,Source.Value)
WHEN NOT MATCHED BY SOURCE THEN 
    DELETE;

这似乎正在做我想要的。任何批评都会有所帮助。对不起,谁会修复这个代码搞砸了。

4

2 回答 2

1
WITH    t2
AS
(
SELECT  * FROM WeatherHourly t
WHERE EXISTS
(
SELECT * FROM #WeatherHourly r WHERE t.EffectiveDateTime = r.EffectiveDateTime AND t.Pk_Id = r.Pk_Id
)
)
MERGE t2
AS Target
USING   #WeatherHourly
AS Source
ON Target.Pk_Id = Source.Pk_Id 
    AND Target.EffectiveDateTime = Source.EffectiveDateTime 
WHEN MATCHED THEN 
    UPDATE SET Target.Value = Source.Value,Target.ObservationDateTime = Source.ObservationDateTime 
WHEN NOT MATCHED BY TARGET THEN
    INSERT ([Pk_Id],[ObservationDateTime],[EffectiveDateTime],[Value]) 
    VALUES (SourcePk_Id,Source.ObservationDateTime,Source.EffectiveDateTime,Source.Value)
WHEN NOT MATCHED BY SOURCE THEN 
    DELETE;

经过测试,这似乎运作良好。

于 2013-05-16T19:07:09.067 回答
0

为什么不删除已有的 EffectiveDateTime,然后插入新的?

DELETE FROM WeatherHourly
WHERE  EffectiveDateTime = ( SELECT TOP(1)
                                  EffectiveDateTime
                              FROM
                                  #WeatherHourly )


INSERT INTO WeatherHourly
(
    [Pk_Id]
    ,[ObservationDateTime]
    ,[EffectiveDateTime]
    ,[Value]
)
SELECT
    [Pk_Id]
    ,[ObservationDateTime]
    ,[EffectiveDateTime]
    ,[Value]
FROM
    #WeatherHourly
于 2013-05-10T21:15:39.483 回答