当前位置: 动力学知识库 > 问答 > 编程问答 >

sql - mysql how to select maximum value of a column as another column in the resultset

问题描述:

My table is like this

ID   VALUE

1     4

2     6

3     12

I want to get an output in this format in mysql, could you provide me with

an appropriate sql for the same

ID   VALUE   MAX_VALUE   DIV_BY_MAX_VALUE

1     4             12                           0.33

2     6             12                           0.5

3     12             12                          1.0

网友答案:

You would join in the maximum and do the division:

select t.*, x.maxvalue, t.value / x.maxvalue
from t cross join
     (select max(value) as maxvalue from t) x;
网友答案:

Try this:

select id,value,(select max(value) from mytable) as max_value, value/(select max(value) from mytable) as division from mytable;
网友答案:
START TRANSACTION;
SET @max_value = (select max(value) from mytable);
Select id, value, @max_value, value/@max_value as div_by_max from mytable;
COMMIT;
分享给朋友:
您可能感兴趣的文章:
随机阅读: