如何获得 PostgreSQL 中的两个平均值之间的差异,平均值在列上,最终表按两列分组?
Posted
技术标签:
【中文标题】如何获得 PostgreSQL 中的两个平均值之间的差异,平均值在列上,最终表按两列分组?【英文标题】:How to get difference between two average values in PostgreSQL, where the averages are on a column, and the final table grouped by two columns? 【发布时间】:2020-10-10 14:59:22 【问题描述】:我想知道value
的两个平均值之间的差异,其中每个平均值都通过条件isCool
过滤为True
或False
,最终结果由town
和@ 分组987654326@,例如
table
| id | value | isCool | town | season |
|----|-------|--------|--------|--------|
| 0 | 1 | True | TownA | spring |
| 1 | 2 | False | TownA | winter |
| 2 | 3 | True | TownB | spring |
| 3 | 4 | False | TownA | winter |
| 4 | 5 | False | TownB | spring |
| 5 | 6 | True | TownB | winter |
我想以表格结束:
| category | difference_of_is_cool_averages |
|----------|--------------------------------|
| TownA | 2 | <-- ABS(1 - (2 + 4)/2)
| TownB | 0.5 | <-- ABS(5 - (3 + 6)/2)
| spring | 3 | <-- ABS(5 - (3 + 1)/2)
| winter | 3 | <-- ABS(6 - (4 + 2)/2)
我已经尝试过了,但是我的 PostgreSQL 技能有限,而且我没有走多远,很遗憾。我试过了
SELECT
AVG(value), town
(SELECT id, value, town, season
FROM table
WHERE isCool = 'True') AS TableSummary1
GROUP BY town;
但这远不是我想要的。请问有人可以帮忙吗?使用 PostgreSQL 甚至可以做到这一点吗?
【问题讨论】:
【参考方案1】:这里Union All
会帮助你。只需将town
分组,然后将season
分组并合并它们,即可计算平均值的差异。您可以编写如下查询:
select
town "Category",
round(abs(avg(value) filter (where iscool='t') - avg(value) filter (where iscool='f')),2) "difference_of_is_cool_averages"
from town
group by town
union all
select
season,
round(abs(avg(value) filter (where iscool='t') - avg(value) filter (where iscool='f')),2)
from town
group by season
DEMO
【讨论】:
【参考方案2】:您可以反透视,然后计算每组的两个条件平均值之间的差异:
select x.category,
abs(avg(t.value) filter(where not t.iscool) - avg(t.value) filter(where t.iscool)) diff
from mytable t
cross join lateral (values (town), (season)) as x(category)
group by x.category
如果您希望能够按照所需结果对结果集进行排序,那么我们需要跟踪原始列:
select x.category,
abs(avg(t.value) filter(where not t.iscool) - avg(t.value) filter(where t.iscool)) diff
from mytable t
cross join lateral (values (town, 1), (season, 2)) as x(category, grp)
group by x.category, x.grp
order by x.grp
Demo on DB Fiddle:
类别 |差异 :------- | ---------------------: 镇B | 0.5000000000000000 镇A | 2.00000000000000000000 冬天| 高分辨率照片| CLIPARTO 3.0000000000000000 弹簧 | 3.0000000000000000【讨论】:
以上是关于如何获得 PostgreSQL 中的两个平均值之间的差异,平均值在列上,最终表按两列分组?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 PostgreSQL 中的两个 IP 获取 CIDR?