mysql feature-scaling calculation

北城余情 提交于 2020-06-16 18:36:35

问题


I need to formulate a mysql query to select values normalized this way: normalized = (value-min(values))/(max(values)-min(values)) My attempt looks like this:

select 
    Measurement_Values.Time, 
    ((Measurement_Values.Value-min(Measurement_Values.Value))/(max(Measurement_Values.Value)-min(Measurement_Values.Value))) 
from Measurement_Values  
where Measurement_Values.Measure_ID = 49 and Measurement_Values.time >= '2020-05-30 00:00'

but is obviously wrong as it returns only one value. Can you help me finding the correct syntax?


回答1:


Your question is a little short on explanations, but I think that you want window functions (available in MySQL 8.0 only):

select 
    time, 
    value,
    (value - min(value) over() / (max(value) over() - min(value) over()) normalized_value
from measurement_values  
where measure_id = 49 and time >= '2020-05-30 00:00'

Or, in earlier versions, you can get the same result by joining the table with an aggregate query:

select 
    mv.time, 
    mv.value,
    (mv.value - mx.min_value) / (mx.max_value - mx.min_value) normalized_value
from measurement_values  
cross join (
    select min(value) min_value, max(value) max_value
    from measurement_values
    where measure_id = 49 and time >= '2020-05-30 00:00'
) mx
where measure_id = 49 and time >= '2020-05-30 00:00'


来源:https://stackoverflow.com/questions/62211117/mysql-feature-scaling-calculation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!