-1

I have a table Customers with one of columns LastActivity. In some cases LastActivity has value NULL and that’s when I need to update it to value which can be found in other table - Activities, where for every customer are few activity records having also timestamp. I need somehow get latest activity for particular customer and update Customer tables’ LastActivity with it. So basically the question is how to update first table with the latest value from second table in case when particular value in first table is NULL.

Martin
  • 1
  • possible duplicate of [Update a table using JOIN in SQL Server?](http://stackoverflow.com/questions/1604091/update-a-table-using-join-in-sql-server) – Tab Alleman Apr 09 '15 at 15:52

1 Answers1

0

For example can be like this:

Update c
    Set c.LastActivity = d.LastActivity
FROM Customers c
INNER JOIN (
        SELECT CustomerID, MAX(LastActivity) AS LastActivity
        FROM AnotherTable
        GROUP BY CustomerID
    ) AS d
    ON d.CustomerID = c.CustomerID
WHERE c.LastActivity IS NULL
Darka
  • 2,762
  • 1
  • 14
  • 31