sql - 在 postgres 中同时计算 AVG 和 stddev_pop 的有效方法
问题描述
stddev_pop() 必须计算 AVG() 作为标准偏差完整计算的一部分(除非有我不知道的快捷方式)。
对于上下文,目标是测试这两个 geom 列之间的均值差异。
有什么方法可以访问它以避免重新计算 AVG()?
这是一个示例查询:
select
avg(st_length(cons.geom)) as source_avg_length,
avg(st_length(csn.geom)) as target_avg_length,
stddev_pop(st_length(cons.geom)) as source_std_length,
stddev_pop(st_length(csn.geom)) as target_std_length
from
received.conflation_osm_no_service cons,
received.conflation_stress_network csn ;
它的输出EXPLAIN ANALYZE
让我觉得如果我要求 avg() 和 stddev_pop() 它只会执行一次 avg() 计算并重用它?:
解决方案
要将两个表组合成一个结果,您必须在加入之前聚合:
select *
from
(
select
avg(st_length(geom)) as source_avg_length,
stddev_pop(st_length(geom)) as source_std_length
from received.conflation_osm_no_service cons
) as src
cross join
(
select
avg(st_length(geom)) as target_avg_length,
stddev_pop(st_length(geom)) as target_std_length,
from
received.conflation_stress_network csn ;
) as tgt
或每张表获得一行:
select 'source' as tablename,
avg(st_length(geom)) as avg_length,
stddev_pop(st_length(geom)) as std_length
from
received.conflation_osm_no_service cons
union all
select 'target',
avg(st_length(geom)),
stddev_pop(st_length(geom)),
from
received.conflation_stress_network csn ;
推荐阅读
- javascript - 阻止变量作为 Javascript 中的函数运行
- postgresql - 由于错误引发的 SQL 异常(sql 代码)(sqlState 22021):用于编码“UTF8”的无效字节序列:0xa2
- google-chrome-devtools - 在 SAML 跟踪器窗口中,红色文本颜色表示什么?
- r - 更新函数中的全局嵌套列表变量,无论深度如何
- linux - 暂停后跳回 2 秒后重新开始在 VLC 中播放视频
- asp.net - 为什么我的帖子在创建时没有加载照片?
- python - Django 电子邮件不会在视图函数中发送
- reactjs - 如何获取对象数组存在的元素
- flutter - 使用 Flutter,如何拖动一个小部件并以另一种方式移动另一个小部件
- swift - 为什么使用 Kotlin Multiplatform 生成的 iOS 框架中的属性名称有下划线(下划线)?