以Postgres中的分组方式显示逐行列值以及相同列的聚合值。

huangapple go评论91阅读模式
英文:

Show row-wise column value as well as the same column's aggregate using group by in Postgres

问题

  1. 我有一些数据,看起来像这样:

students:

  1. id score section ...
  2. --------------------------------------------
  3. 1 85 A ...
  4. 2 40 A ...
  5. 3 61 B ...
  6. 4 71 B ...
  7. 5 80 B ...
  1. 我想要显示每个学生的分数以及他们所在部分的最高分。

students:

  1. id score section section_max_score ...
  2. --------------------------------------------
  3. 1 85 A 85 ...
  4. 2 40 A 85 ...
  5. 3 61 B 80 ...
  6. 4 71 B 80 ...
  7. 5 80 B 80 ...
  1. 我尝试做这样的事情:

select id, score, section, max(score) as section_max_score from students group by section;

  1. 这会导致错误,要求我在group by中包括其他列。

select id, score, section, max(score) as section_max_score from students group by section, score, id;

  1. 这只显示每个学生的个别分数,而不是部分的最高分。
  2. 我漏掉了什么,如何修复它?
  3. 实际上,我需要能够在group by查询中使用特定列以及对该列进行聚合操作。
英文:

I have some data that looks like this:

  1. students:
  2. id score section ...
  3. --------------------------------------------
  4. 1 85 A ...
  5. 2 40 A ...
  6. 3 61 B ...
  7. 4 71 B ...
  8. 5 80 B ...

I would like to show the score each student along with the maximum score of their section.

  1. students:
  2. id score section section_max_score ...
  3. --------------------------------------------
  4. 1 85 A 85 ...
  5. 2 40 A 85 ...
  6. 3 61 B 80 ...
  7. 4 71 B 80 ...
  8. 5 80 B 80 ...

I am trying to do something like this:

  1. select id, score, section, max(score) as section_max_score from students group by section;

This gives an error that I need to include other columns in the group by.

  1. select id, score, section, max(score) as section_max_score from students group by section, score, id;

This just shows the individual score of each student instead of the section maximum.

What am I missing and how do I fix it?

Essentially, I need to be able to use a particular column as well an aggregate on that column in a group by query.

答案1

得分: 2

你可以使用窗口函数 max()

  1. select id, score, section, max(score) over ( partition by section) as section_max_score
  2. from students

或者使用 inner join 将你的表与每个部分的最大分数数据集连接起来:

  1. select s.*, t.section_max_score
  2. from students s
  3. inner join (
  4. select section, max(score) as section_max_score
  5. from students
  6. group by section
  7. ) as t on s.section = t.section

演示在此处

英文:

You can use the window function max() :

  1. select id, score, section, max(score) over ( partition by section) as section_max_score
  2. from students

Or use an inner join to join your table to a dataset of maximum score per section:

  1. select s.*, t.section_max_score
  2. from students s
  3. inner join (
  4. select section, max(score) as section_max_score
  5. from students
  6. group by section
  7. ) as t on s.section = t.section

Demo here

huangapple
  • 本文由 发表于 2023年5月25日 17:34:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定