PostgreSQL 视图:将记录扁平化为列
我想创建一个视图来显示有关系统用户的有趣内容。除此之外,我的数据库有一个表,每个用户有几行,基本上从 0 到 3 行不等。每个这样的行都有一个名为“name”的字符串字段,我希望我的视图包含所有这些以逗号分隔的内容。例如:
UID Name Concatenation
1 John A, C
2 Jack B, C
3 James
4 Jill B
有没有办法从其他表中选择到这一列?我正在使用 PostgreSQL,但这对我来说是一个通用的 SQL 问题。
I want to create a view that will display interesting stuff about the system users. Among other things, my DB has this table that has several rows per user, ranging basically from 0 to 3 rows. Each such row has a string field called "name", and I'd like my view to contain all of these comma-separated. For example:
UID Name Concatenation
1 John A, C
2 Jack B, C
3 James
4 Jill B
Is there a way to select from the other table into this column? I'm using PostgreSQL but this strikes me as a generic SQL question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此处:
如何在 PostgreSQL 'group by' 查询中连接字符串字段的字符串?
您应该定义一个新的聚合函数。确切的函数取决于您的表结构,但这里有 来自 PostgreSQL 的示例forums:
您可以在查询中使用这个新聚合。例如:
See here:
How to concatenate strings of a string field in a PostgreSQL 'group by' query?
You should define a new aggregate function. The exact function depends on your table structure, but here's an example from the PostgreSQL forums:
You can use this new aggregate in your query. For example:
例如,您可以使用某种字符串连接聚合:
但您需要 9.0 如果您想在聚合中使用
order by
子句you can use some sort of string concatenation aggregate for example:
but you need 9.0 if you want to use an
order by
clause in the aggregate