简体   繁体   中英

Calculate Median for each group in AWS Athena table

Below is the schema for the athena table

在此处输入图片说明

I wish to calculate median for 'parameter_value' group by standard_lab_parameter_name & units. For this I followed link : https://docs.aws.amazon.com/redshift/latest/dg/r_MEDIAN.html But on running the query

select median(parameter_value) from table_name group by standard_lab_parameter_name, units

It throws error

 SYNTAX_ERROR: line 1:8: Function median not registered

Any help? Or if some alternative query would be great

Athena is based on Presto 0.172 - you can see all supported functions in AWS DML Queries, Functions, and Operators . I guess you could use approx_percentile(x, percentage) or approx_percentile(x, w, percentage, accuracy) , see Presto Aggregate Functions :

Returns the approximate percentile for all input values of x at the given percentage. The value of percentage must be between zero and one and must be constant for all input rows.

select approx_percentile(parameter_value,0.5) 
from table_name 
group by standard_lab_parameter_name, units

Keep in mind that this is a Approximate Aggregate Functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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