postgresql - Postgres 获取具有逗号分隔值的列表
问题描述
我正在研究 Postgres SQL 并具有以下联接查询,当我执行此查询时,我会为每个员工获得两个或 3 条记录,因为每个员工都有 3 种不同类型的电子邮件地址 1)家庭地址 2)办公室地址 3)社交地址。
select * from root.employee c
full outer join root.employee_email ce
on c.employee_id = ce.employee_id
order by c.employee_id limit 1000 offset 0;
我想要的是该employee_email.email
列在输出中给出逗号分隔值,我们该怎么做?
注意:我在 DB 中有近 100 万条记录,将使用 Spring Batch 将数据从 Postgres 迁移到 MongoDB。我需要为电话做同样的事情
解决方案
按员工汇总并使用string_agg
:
select
c.employee_id, -- or just c.* assuming employee_id is a PK
string_agg(ce.email, ',') as emails
from root.employee c
full outer join root.employee_email ce
on c.employee_id = ce.employee_id
group by
c.employee_id
order by
c.employee_id
limit 1000
offset 0;
推荐阅读
- ruby-on-rails - 当我从 After hook 调用 erb 程序时,没有呈现 Html 文件
- java - Google API 不适用于 Spring Boot 和 Maven
- python - 如何在 python 中使用 matplotlib 设置 x 轴刻度线标签?
- keras - 如何纠正错误 ImportError: cannot import name 'MobileNet'?
- c - 如何在C中的给定地址动态分配内存?
- python-3.x - 如何对文本数据(IMDB 评论)执行回归算法?
- python - 在函数中更改具有相似名称的变量值
- python - matplotlib 动画保存不遵守 blit=True 但它似乎在 plt.show() 中工作得很好
- java - bignumber 的 BigDecimal 在浮点值中无法正常工作
- java - 如何有效地计算数组项之间的差异并找出有多少差异小于某个值?