饼子求个sql
本帖最后由 sixfish 于 2013-05-07 13:21:05 编辑
有三个表 省份销售,城市销售,城市份额
我想更新城市销售,就是用该城市所属的省份的销售额salesamount乘份额percent
城市表CITY(cityNO,salesamount)
省份表PREF(prefNO,salesamount)
城市份额PCENT(cityNO,percent)
cityNO的前两位是prefNO,即prefNO等substring(cityNO,1,2)、
求一个sql更新
------解决方案--------------------
update a set a.salesamount = c.prefNO*b.percent from CITY a
join PCENT b on b.cityNO = a.cityNO
join PREF c on substring(b.cityNO,1,2) = c.prefNO
------解决方案--------------------
update a
set a.salesamount=c.salesamount*b.percent
from CITY a
inner join PCENT b on a.cityNO=b.cityNO
inner join PREF c on substring(a.cityNO,1,2)=c.prefNO
------解决方案--------------------update CITY
set salesamount = summount
from
(
select p.prefNO,( p.salesamount * c.percent )summount from PREF p,PCENT c
where p.prefNO = c.substring(cityNO,1,2)
)b where CITY.prefNO=b.prefNO