简体   繁体   English

将聚合 function 应用于 table 上的所有列,按 group by

[英]Apply aggregate function to all columns on table with group by

I am trying to select all the columns which will be the same based on the grouping我正在尝试 select 基于分组将相同的所有列

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 |
+------+-------+---------+----------+-----------+--------------+

The output that I am looking for would be:我正在寻找的 output 将是:

+------+-------+---------+----------+-----------+--------------+
| 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 |
+------+-------+---------+----------+-----------+--------------+

I know this isn't right, but my the general thought process is:我知道这是不对的,但我的一般思考过程是:

select
   min(*),
   sum(score)
from test_table
group by age, name

I want to avoid doing something like:我想避免做类似的事情:

select 
  min(col1),
  min(col2),
  ... cont ...,
  min(col50),
  sum(score)
from ...

You can't avoid listing all the columns individually.您无法避免单独列出所有列。 Also, if all the columns where you are using min have the same values for each combination of group by columns, then using min will be very inefficient - just list them in your select and group by clauses此外,如果您使用 min 的所有列对于 group by 列的每个组合都有相同的值,那么使用 min 将非常低效 - 只需在 select 和 group by 子句中列出它们

You can use DISTINCT ON to get one row per group and join that with total scores calculated by a GROUP BY query.您可以使用DISTINCT ON为每个组获取一行,并将其与GROUP BY查询计算的总分数连接起来。 With this approach there will be score column containing value from some row in a group and a separate column for total score.使用这种方法,将有一个包含来自组中某行的值的score列和一个单独的总分列。

WITH total_scores AS (
    SELECT age, name, SUM(score) AS total_score
    FROM test_table
    GROUP BY age, name
)
SELECT DISTINCT ON (tt.age, tt.name)
    tt.*, ts.total_score
FROM test_table tt
JOIN total_scores ts ON tt.age = ts.age AND tt.name = ts.name

That said, it seems that you could normalize your data into two tables, one containing rows that have duplicate values (ie everything else except score ) and another table containing score and a foreign key pointing to the first table.也就是说,您似乎可以将数据规范化为两个表,一个包含具有重复值的行(即除score之外的所有其他内容),另一个包含score和指向第一个表的外键的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM