PostgreSQL:如何汇总文本,以便在同一行中显示? (通过ID分组)

发布于 2025-02-01 10:12:14 字数 903 浏览 4 评论 0原文

从一组学生的名字中,我想确定每个ID的学生执行哪个步骤。

这是我得到的输出:

id step1    step2    step3
1  Sam
1           John
2  Ana      
2           Charlie
2                     Bob
3           Alex

我使用代码获得的输出:

select id,
     case when step = 'A' then student_name end as "step1",
     case when step = 'B' then student_name end as "step2",
     case when step = 'C' then student_name end as "step3"
from my_table
group by id

这是我想获得的输出:

id step1    step2    step3
1  Sam      John
2  Ana      Charlie  Bob
3           Alex

如果名称是数字,我会做:

select id,
     sum(case when step = 'A' then student_name end) as "step1",
     sum(case when step = 'B' then student_name end) as "step2",
     sum(case when step = 'C' then student_name end) as "step3"
from my_table
group by id

但是,这不能用文本执行。如何修改查询以实现上述输出?

From a group of student names, I'd like to identify which student performed which step per id.

This is the output I get:

id step1    step2    step3
1  Sam
1           John
2  Ana      
2           Charlie
2                     Bob
3           Alex

which I obtain with the code:

select id,
     case when step = 'A' then student_name end as "step1",
     case when step = 'B' then student_name end as "step2",
     case when step = 'C' then student_name end as "step3"
from my_table
group by id

This is the output I'd like to get:

id step1    step2    step3
1  Sam      John
2  Ana      Charlie  Bob
3           Alex

If the names were numbers, I'd do:

select id,
     sum(case when step = 'A' then student_name end) as "step1",
     sum(case when step = 'B' then student_name end) as "step2",
     sum(case when step = 'C' then student_name end) as "step3"
from my_table
group by id

However, this cannot be performed with text. How can I modify the query to achieve the output above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-02-08 10:12:14

您的数据

CREATE TABLE mytable(
   id    INTEGER  
  ,step1 VARCHAR(30) 
  ,step2 VARCHAR(30)
  ,step3 VARCHAR(30)
);
INSERT INTO mytable
(id,step1,step2,step3) VALUES 
(1,'Sam',NULL,NULL),
(1,NULL,'John',NULL),
(2,'Ana',NULL,NULL),
(2,NULL,'Charlie',NULL),
(2,NULL,NULL,'Bob'),
(3,NULL,'Alex',NULL);

只需使用max函数

SELECT id,
       Max(step1) step1,
       Max(step2) step2,
       Max(step3) step3
FROM   mytable
GROUP  BY id
ORDER  BY id ASC  

your data

CREATE TABLE mytable(
   id    INTEGER  
  ,step1 VARCHAR(30) 
  ,step2 VARCHAR(30)
  ,step3 VARCHAR(30)
);
INSERT INTO mytable
(id,step1,step2,step3) VALUES 
(1,'Sam',NULL,NULL),
(1,NULL,'John',NULL),
(2,'Ana',NULL,NULL),
(2,NULL,'Charlie',NULL),
(2,NULL,NULL,'Bob'),
(3,NULL,'Alex',NULL);

just use max function

SELECT id,
       Max(step1) step1,
       Max(step2) step2,
       Max(step3) step3
FROM   mytable
GROUP  BY id
ORDER  BY id ASC  

dbfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文