如何在 postgres sequelize 中按位置分组获取所有用户列表?
How to get all user list group by location in postgres sequelize?
用户名
位置
用户 1
L1
用户 2
L2
用户 3
L2
用户 4
L3
用户 5
L4
我需要最终输出
L1 - ['user1']
L2 - ['user2', 'user3']
L3 - ['user4']
L4 - ['user5']
你可以在 postgres 中使用 array_agg
:
select location , array_agg(username)
from table
group by location
或者,如果您想要逗号分隔的字符串而不是列表,您可以使用 string_agg(username,',')
db<>fiddle here
用户名 | 位置 |
---|---|
用户 1 | L1 |
用户 2 | L2 |
用户 3 | L2 |
用户 4 | L3 |
用户 5 | L4 |
我需要最终输出
L1 - ['user1']
L2 - ['user2', 'user3']
L3 - ['user4']
L4 - ['user5']
你可以在 postgres 中使用 array_agg
:
select location , array_agg(username)
from table
group by location
或者,如果您想要逗号分隔的字符串而不是列表,您可以使用 string_agg(username,',')
db<>fiddle here