Update table without using Cursor
I have data like this
alt text http://img252.imageshack.us/img252/8518/data.png
I want to update minP, maxP and agvP columns on the basis of Id. My desired result is;
alt text http://img20.imageshack.u开发者_Python百科s/img20/2029/updatedu.png
Please help.
Try something like:
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
精彩评论