SQL更新问题

SQL更新问题,sql,Sql,表格名称:历史表格 Shp_cd wt_grp net_wt tot_wt 101 10 9 7 102 20 8 2 103 15 4 1 因子表 Fact_id fact_column factor 1 wt_grp 2 2 net_wt 5 3 tot_wt 3 注意–此系数表包含作为hist_表列名的行 现

表格名称:历史表格

 Shp_cd wt_grp  net_wt  tot_wt
   101   10       9      7
   102   20       8      2
   103   15       4      1
因子表

Fact_id fact_column factor
  1     wt_grp        2
  2     net_wt        5
  3     tot_wt        3
注意–此系数表包含作为hist_表列名的行

现在我们必须用乘以因子的值来更新Hist_表

例如,wt_grp factor为2,那么我们必须将hist表的所有wt_grp列更新为 Hist_table.wt_grp*因子_table.factor

所以结果应该是

shpcd   wt_grp  net_wt  tot_wt
101     20        45    21
102     40        40    6
103     30        20    3

请让我知道更新此查询的一种方法是在两个表之间执行交叉连接(生成笛卡尔积),然后有选择地应用所需的计算

然后可以使用
MAX()


您可以执行以下操作

-- update wt_grp
update h set h.wt_grp=h.wt_grp*f.factor
from Hist_table h,Factor_table f 
where f.fact_column='wt_grp'

-- update net_wt  
update h set h.net_wt=h.net_wt*f.factor
from Hist_table h,Factor_table f 
where f.fact_column='net_wt'

-- update tot_wt
update h set h.tot_wt=h.tot_wt*f.factor
from Hist_table h,Factor_table f 
where f.fact_column='tot_wt'


select * from Hist_table
这是一张工作票


希望这能对你有所帮助

我认为你的乘法运算是错误的,或者你的计算是错误的。每次你将它乘以2,而结果应该是20,100,45。@AnkitBajpai,计算很好:当我阅读需求时,每列都有一个与之相关的特定乘数。总重量始终为2,净重量始终为5,以此类推。
-- update wt_grp
update h set h.wt_grp=h.wt_grp*f.factor
from Hist_table h,Factor_table f 
where f.fact_column='wt_grp'

-- update net_wt  
update h set h.net_wt=h.net_wt*f.factor
from Hist_table h,Factor_table f 
where f.fact_column='net_wt'

-- update tot_wt
update h set h.tot_wt=h.tot_wt*f.factor
from Hist_table h,Factor_table f 
where f.fact_column='tot_wt'


select * from Hist_table