首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过滤掉重复项,但在一行中显示另一列中的非重复行

过滤掉重复项,但在一行中显示另一列中的非重复行
EN

Stack Overflow用户
提问于 2019-08-10 21:49:03
回答 1查看 33关注 0票数 1

我有一个包含多个重复值的表。我想要做的是使用某种SELECT查询,该查询将按名称过滤重复项,同时保留"expertise“列中的所有适当值,并将它们显示在由coma分隔的一行中。

换句话说,我想转一张桌子,看起来像这样:

代码语言:javascript
复制
 names   | organization | education_years | expertise 
---------+--------------+-----------------+------------------------------------------------------------------------------------------------------------------------------------------
 John    | University   | 2016 – 2020     | Programming
 John    | University   | 2016 – 2020     | machine Learning
 John    | University   | 2016 – 2020     | AI
 John    | University   | 2016 – 2020     | Robotics
 John    | University   | 2016 – 2020     | Symbolic Reasoning
 Gabriel | Company      | 2015 – Present  | models/networks
 Gabriel | Company      | 2015 – Present  | AI
 Gabriel | Company      | 2015 – Present  | Speech/language
 Gabriel | Company      | 2015 – Present  | machine learning
 Gabriel | Company      | 2015 – Present  | Programming
 Nicolas | Company      | Present         | machine learning, factorization, learning theory, supervised learning

如下所示:

代码语言:javascript
复制
 names   | organization | education_years | expertise 
---------+--------------+-----------------+------------------------------------------------------------------------------------------------------------------------------------------
 John    | University   | 2016 – 2020     | Programming, machine Learning, AI, Robotics, Symbolic Reasoning
 Gabriel | Company      | 2015 – Present  | models/networks, AI, Speech/language, machine learning, Programming
 Nicolas | Company      | Present         | machine learning, factorization, learning theory, supervised learning

我使用PostgreSQL作为我的数据库。

有人能帮上忙吗?我已经很久没有做过查询了,也找不到一个类似的问题来解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-10 21:56:15

使用聚合:

代码语言:javascript
复制
select name, organization, education_years,
       string_agg(expertise, ', ')
from t
group by name, organization, education_years;

上面使用了string_agg(),因为结果看起来像一个字符串。我建议您改用数组:

代码语言:javascript
复制
select name, organization, education_years,
       array_agg(expertise)
from t
group by name, organization, education_years;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57442734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档