简体   繁体   English

用最小日期更新临时表

[英]Update temp table with Min Date

I'm trying to update the InsertDate for all the distinct AccId and AccNameHolder combinations in the #tmpResults table only if an earlier insert date exists. 仅在存在较早的插入日期的情况下,我才尝试为#tmpResults表中的所有不同AccIdAccNameHolder组合更新InsertDate

The table schemas are like so: 表模式如下所示:

#tmpResults #tmp结果

Trans | AccId | AccountNameHolder | EarlyDate | CardType 

PositionCollection PositionCollection

Id | AccId | InsertDate | BtchId

CardErrors CardErrors

AccId | AccNameHolder | BtchId | TransId

Here is what I have tried so far: 到目前为止,这是我尝试过的:

UPDATE  RES  
SET     EarlyDate = pc.InsertDate   
FROM  ( dbo.PositionCollection pc    
        LEFT JOIN dbo.CardErrors ce ON ce.BtchId = pc.BtchId AND pc.id =     ce.Transid  )    
    INNER JOIN #tmpResults RES   
ON      RES.AccId = pc.AccId    
AND     RES.AccNameHolder = ce.AccNameHolder    
WHERE   RES.EarlyDate = (SELECT MIN(InsertDate) FROM PostionCollection)    
AND     RES.AccId = pc.AccId    
AND     RES.AccNameHolder = ce.AccNameHolder

Heres a few more general solutions, for where InsertDate is null, You can use them with DateDiff to change the conditions to earlier, later or whatever or just something like 这是一些更通用的解决方案,对于InsertDate为null的情况,您可以将它们与DateDiff一起使用,以将条件更改为更早,更高或任何类似的条件

OldInstDate <= COALESCE(NewInstDate, OldInstDate)

The conditional update can be achieved within the select statement so... 可以在select语句中实现条件更新,因此...

SET InsertDate = COALESCE(InsertDate, NewInsertDate) WHERE...

OR 要么

SET InsertDate = CASE WHEN InsertDate IS NULL THEN NewInsertDate 
                      ELSE InsertDate END

OR 要么

WHERE ...
AND InsertDate IS NULL

For really clear help on sub queries http://allenbrowne.com/subquery-01.html I think this is probably the best I've seen and http://allenbrowne.com/subquery-02.html for trouble shooting 对于子查询http://allenbrowne.com/subquery-01.html的真正清晰的帮助,我认为这可能是我见过的最好的查询了, http://allenbrowne.com/subquery-02.html可以解决问题

Use derived table in your UPDATE statement 在UPDATE语句中使用派生表

UPDATE x
SET x.EarlyDate = x.InsertDate  
FROM  ( 
       SELECT RES.EarlyDate, pc.InsertDate
       FROM dbo.PositionCollection pc
         LEFT JOIN dbo.CardErrors ce ON ce.BtchId = pc.BtchId AND pc.id = ce.Transid
         INNER JOIN #tmpResults RES ON RES.AccId = pc.AccId
           AND RES.AccNameHolder = ce.AccNameHolder
       WHERE RES.EarlyDate = (SELECT MIN(InsertDate) FROM PostionCollection) AND RES.AccId = pc.AccId
         AND RES.AccNameHolder = ce.AccNameHolder
       ) x

UPDATE 27.11.2012 更新27.11.2012

UPDATE x
SET x.EarlyDate = x.InsertDate
FROM  (
       SELECT RES.EarlyDate, m.InsertDate
       FROM dbo.PositionCollection pc
         LEFT JOIN dbo.CardErrors ce ON ce.BtchId = pc.BtchId AND pc.id = ce.Transid
         INNER JOIN #tmpResults RES ON RES.AccId = pc.AccId AND RES.AccNameHolder = ce.AccNameHolder
         INNER JOIN (SELECT MIN(InsertDate) AS InsertDate FROM PostionCollection) m
           ON RES.EarlyDate = m.InsertDate 
       ) x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM