【发布时间】:2022-01-13 20:45:13
【问题描述】:
我正在尝试根据分组选择所有相同的列
test_table
+------+-------+---------+----------+-----------+--------------+
| age | name | score | col1 | col2 | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 20 | joe | 10 | DING | DONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 20 | joe | 20 | DING | DONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 22 | sue | 25 | SING | SONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 22 | sue | 10 | SING | SONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 50 | bob | 25 | RING | WRONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 44 | joe | 15 | THING | THONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
我正在寻找的输出是:
+------+-------+---------+----------+-----------+--------------+
| age | name |sum(score| col1 | col2 | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 20 | joe | 30 | DING | DONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 22 | sue | 35 | SING | SONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 50 | bob | 25 | RING | WRONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
| 44 | joe | 15 | THING | THONG | col3...col50 |
+------+-------+---------+----------+-----------+--------------+
我知道这是不对的,但我的一般思考过程是:
select
min(*),
sum(score)
from test_table
group by age, name
我想避免这样做:
select
min(col1),
min(col2),
... cont ...,
min(col50),
sum(score)
from ...
【问题讨论】:
标签: sql database postgresql group-by