sql - 将同一表中的行分组为同一表中的一行

标签 sql postgresql

表A

  • group_id
  • 我的帐号
  • 值(value)

假设我有

group_id     my_id    value
  1            a        10
  1            b        15
  2            c        20
  3            d        25

我要输出

   group_id     my_id    value    family
      1            a        10     [10, 15]
      1            b        15     [10, 15]
      2            c        20     [20]
      3            d        25     [25]

最佳答案

使用 array_agg 并加入

select t1.*,t2.family from tbl t1
  join 
    (
    select group_id,array_agg(value) as family form tbl
    group by group_id
    ) as t2 t1.group_id=t2.group_id

关于sql - 将同一表中的行分组为同一表中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184345/

相关文章:

php - Symfony2 & Doctrine : Create custom SQL-Query

sql - 寻找一种更有效的方法将 IP 地址分解为八位字节

postgresql - 在 Postgres 存储过程中传递表值参数

SQLGrammer异常 : could not execute statement

node.js - Sequelize : Use multiple keys from source to include target model

postgresql - 模糊字符串匹配 : which tool?

Java MySQL NullPointerException 查询

c# - 通过 dapper 从 sql 存储过程插入返回 -1 (不是 "working")

sql - 带有 NOT IN 的子查询

windows - 如何 : Inform application that database table row is updated?