14
 insert into tableA (column1) 
 select min(tableC.column1)
 from tableB 
 inner join tableC on (tableC.coumn2 = tableB.column1
 and tableB.column2 = tableA.column2) 
 group by tableA.column2

如何根据条件将上述内容更改为使用 group by 进行更新,而不是使用 group by 插入tableB.column2 = tableA.column2

请注意,我正在使用SQL SERVER 2008.

4

1 回答 1

31
  Update A set Column1 = minC    
    from (select Ab.Column2, min(C.Column1) as minC
            from A Ab
            inner join B on Ab.Column2 = B.Column2
            inner join C on C.column2 = B.Column2 --No need to add again the A.col2 = B.col2
            group by Ab.Column2) Grouped where A.Column2 = Grouped.Column2

这是你想要的吗?这将为每个 columnA 获取C.Column1最小值,并将A.Column1根据 condition 更新它(这是您之前插入的位置)A.Column2 = Grouped.Column2

这是一个SQL-Fiddle 演示

于 2012-09-09T23:02:14.793 回答