amazon-redshift - 在与平均值相同的查询中使用中位数聚合?
问题描述
有没有办法解决这个错误?
不支持将 LISTAGG/PERCENTILE_CONT/MEDIAN 聚合函数与其他不同的聚合函数一起使用
我的代码:
select
date,
count(distinct listing_id) as count_listings,
count(distinct inquiry_id) as count_inquiries,
avg(days_btw_start_date_first_inquiry) as avg_days_btw_start_date_first_inquiry,
median(days_btw_start_date_first_inquiry) as med_days_btw_start_date_first_inquiry
from together
group by 1
中位数抛出错误,但我希望在一张表中有计数+平均+中位数。
谢谢!
解决方案
作为一种解决方法,您可以加入 2 个分组结果
select
t.date,
avg(days_btw_start_date_first_inquiry) as avg_days_btw_start_date_first_inquiry,
median(days_btw_start_date_first_inquiry) as med_days_btw_start_date_first_inquiry,
t2.count_listings,
t2.count_inquiries
from together t
left join (
select date,
count(distinct listing_id) as count_listings,
count(distinct inquiry_id) as count_inquiries
from together
) t2 on t.date = t2.date
group by 1
如果您的数据量不以 TB 计,那么它不会对性能产生非常显着的影响。对于 50GB 的表和非常相似的查询,这种解决方法对没有连接和不同函数的查询产生 +20% 的运行时间影响。
推荐阅读
- python - 使用 python `mutliprocessing.Process().daemon=True` 创建的进程是真正的守护进程吗?
- python - 将错误传递给django中views.py中的重定向模板
- mongodb - Rest api - MongoNetworkError:无法连接到服务器
- python - 在模块之间导入全局变量
- docker - 为什么 Graphite 安装需要 docker
- azure-devops - 在 vsts 扩展中如何从 url 参数中获取值?
- maven - CSV 格式的 Maven Surefire 报告
- c# - 正则表达式在 C# 中搜索不以“0”开头的节号
- android - 自定义适配器后无法在 Activity 中启用按钮编辑文本 afterTextChanged()
- python-3.x - 在 Python3 中读取 NetCDF 文件