如何将列值设置为另一列的列组的最大值

时间:2014-10-07 11:36:56

标签: sql db2 sybase

要求....
A列| B栏 1个| 23个
1个| 25个
1个| 26个
2个| 4个
2个| 5个
2个| 6

我想要更新B列,并且应该将值设置为A组的最大值。结果应该是

A栏| B栏 1个| 26个
1个| 26个
1个| 26个
2个| 6个
2个| 6个
2个| 6

2 个答案:

答案 0 :(得分:1)

尝试使用相关子查询:

update table t
    set columnb = (select max(columnB) from table t2 where t2.columnA = t.columnA);

答案 1 :(得分:0)

首先,您需要使用group by来计算max值并将其存储在临时表中:

select  columnA,
        max(columnB) as maxB
into    #TMP
from    table

然后您可以使用简单的连接进行更新:

update  table
set     columnB = maxB
from    #TMP
where   table.columnA = #TMP.columnA

希望有所帮助。

-Dave

相关问题