← Tutti gli articoli

How-To Update Tables Using Inner Joins and Group By

05 marzo 2011  · TSQL · Recipe  ·  165 visite

I'm making an update to my table Contents and would like to update the ContentDate coloumn in all the rows that have the minimum BeginDate in the ContentsHistory table.

UPDATE c
  SET c.ContentDate = hh.NewDate
  
  FROM
  Contents as c 
  INNER JOIN 
  (Select ContentId, MIN(BeginDate) as NewDate from ContentsHistory group by ContentId ) as hh
  
  on c.ContentId = hh.ContentId