Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

mysql

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
like image 479
user2346266 Avatar asked Dec 15 '25 20:12

user2346266


2 Answers

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;
like image 164
Gordon Linoff Avatar answered Dec 17 '25 18:12

Gordon Linoff


Try this:

select id,value,(select max(value) from mytable) as max_value, value/(select max(value) from mytable) as division from mytable;
like image 34
Priyansh Goel Avatar answered Dec 17 '25 17:12

Priyansh Goel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!