我有一个查询,其中一个列的类型为date,另一个列的类型为double precision。date列称为trading_day,double precision列称为stock_price。我想要一年2000中每个月的stock_price中值。
我的疑问如下:
select date_trunc('month',trading_day) as dt_trunc,median(stock_price) from data_price
where extract(year from trading_day) = 2000
group by dt_trunc我不确定我的查询是否真的是正确的聚合和计算,我还认为在较新版本的PostgreSQL中存在中值函数。
发布于 2021-11-14 12:58:28
计算中位数的标准SQL方法是获得50%的连续百分位数。
select
date_trunc('month', trading_day) as dt_trunc
, percentile_cont(0.5) WITHIN GROUP (ORDER BY stock_price) as median
from data_price
where extract(year from trading_day) = 2000
group by dt_trunchttps://stackoverflow.com/questions/69962973
复制相似问题