不使用Cursor更新表

时间:2009-11-02 21:21:26

标签: sql sql-server-2005

我有这样的数据

alt text http://img252.imageshack.us/img252/8518/data.png

我想在Id的基础上更新minP,maxP和agvP列。我想要的结果是;

alt text http://img20.imageshack.us/img20/2029/updatedu.png

请帮忙。

1 个答案:

答案 0 :(得分:4)

尝试类似:

update mt
set minp = a.minp,
maxp = a.maxp,
avgp = a.avgp
from mytable mt
join
(select id, min(price) as minp, max(price) as maxp, avg(price) as avgp
from mytable group by id) a on a.id = mt.id