简体   繁体   English

用平均值计算更新列

[英]Update column with average calculation

Original atributes in table boat(bID, name, legth, colour) 船的原始属性(出价,名称,长度,颜色)

Then i create a new atribute called relLength ant i need to to write sentences, where I will update my column (which set to 0 nowstrong text) with this formula: 然后,我需要编写一个新的名为relLength ant的属性,我需要写一些句子,在其中用以下公式更新我的列(该列设置为0 nowstrong文本):

relLength = Length/AVERAGE length. relLength =长度/平均长度。

I have this atributes in this table called boat: bID, name, length, colour, relLength (this was created) 我在此表中将这个属性称为boat:bID,名称,长度,颜色,relLength(已创建)

This Is what I wrote, but is not working 这是我写的,但是没有用

UPDATE boat
SET relLength = length/  (SELECT AVG(length) FROM boat)

(the average is 38.75) (平均38.75)

The other exercises is about to create view. 其他练习将创建视图。 I need to take original table (without relLengt inside) and create view where i create this (relLength) atribute and fill with previous formula only in view table and not into table. 我需要获取原始表(内部没有relLengt)并创建视图(我在其中创建此(relLength)属性),并仅在视图表中而非表中填充以前的公式。

Thanks for your help. 谢谢你的帮助。 :) :)

Try the following: 请尝试以下操作:

update boat b, (select avg(length) avg_len from boat) v
set
  b.rellength = v.avg_len

Thanks a lot. 非常感谢。

Almost what i've been looking for. 几乎是我一直在寻找的东西。 Just one minor correction ( length /v.avg_len in the last line) 只需进行一次小更正(最后一行的长度为 /v.avg_len)

this is what works now for me: 这对我现在有效:

update boat b, (select avg(length) avg_len from boat) v
set
  b.rellength = length / v.avg_len

Can anyone create view? 任何人都可以创建视图吗? I need to create view from original table boat(bID, name, legth, colour) and create column rellength (like i did this with alter table...) and calculete like it is written above (atribute rellength doesn't exist in original tabel, only in view, where a have this atributes: bID, name, legth, colour, rellength !) 我需要从原始表船(bID,名称,长度,颜色)创建视图,并创建列rellength(就像我用alter table做的那样...)并像上面写的一样计算(属性rellength在原始文件中不存在表格,仅在视图中具有a的属性:bID,名称, 长度 ,颜色, 相对长度 !)

Thanks again :)) 再次感谢 :))

I did find the solution for view. 我确实找到了解决方案以供查看。

CREATE OR REPLACE VIEW newBoat
AS SELECT b.bid, b.name, b.length, b.color, b.length/(
                            select avg(length) 
                            from boat b) as relLengt
FROM boat b;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM