【发布时间】: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 甚至可以做到这一点吗?
【问题讨论】:
标签: sql postgresql average unpivot