From multiple rows to single column in Postgres

狂风中的少年 提交于 2020-01-23 13:00:27

问题


I am using Postgres 9.5 via pgAdmin 4 with read only access and im trying to write a select query that converts data from this form:

+----------+-------------------+--------------------+----------------+
| username |      filters      |       groups       |     roles      |
+----------+-------------------+--------------------+----------------+
| kd24     | Khaled  <27607>   | V1                 | NewsStand User |
| kd24     | Khaled  <27607>   | V1                 | User           |
| kd24     | Khaled  <27607>   | Weekly KPIs        | NewsStand User |
| kd24     | Khaled  <27607>   | Detailed Sales     | User           |
| kd24     | Khaled  <27607>   | Qanz Monthly Group | User           |
| kd24     | Khaled  <27607>   | Detailed Sales     | NewsStand User |
| kd24     | Khaled  <27607>   | Qanz Monthly Group | NewsStand User |
| kd24     | Khaled  <27607>   | Weekly KPIs        | User           |
| kd24     | Khaled  <F_27607> | Weekly KPIs        | User           |
| kd24     | Khaled  <F_27607> | Weekly KPIs        | NewsStand User |
| kd24     | Khaled  <F_27607> | Qanz Monthly Group | NewsStand User |
| kd24     | Khaled  <F_27607> | Detailed Sales     | User           |
| kd24     | Khaled  <F_27607> | V1                 | User           |
| kd24     | Khaled  <F_27607> | Detailed Sales     | NewsStand User |
| kd24     | Khaled  <F_27607> | Qanz Monthly Group | User           |
| kd24     | Khaled  <F_27607> | V1                 | NewsStand User |
| kd24     | khaled.d          | Weekly KPIs        | User           |
| kd24     | khaled.d          | V1                 | NewsStand User |
| kd24     | khaled.d          | Detailed Sales     | NewsStand User |
| kd24     | khaled.d          | Qanz Monthly Group | NewsStand User |
| kd24     | khaled.d          | Weekly KPIs        | NewsStand User |
| kd24     | khaled.d          | V1                 | User           |
| kd24     | khaled.d          | Detailed Sales     | User           |
| kd24     | khaled.d          | Qanz Monthly Group | User           |
+----------+-------------------+--------------------+----------------+

To this form:

+----------+-----------------------------------------------+-----------------------------------------------------+---------------------+
| username |                    filters                    |                       groups                        |        roles        |
+----------+-----------------------------------------------+-----------------------------------------------------+---------------------+
| kd24     |  Khaled  <27607>, Khaled  <F_27607>,khaled.d  | V1, Weekly KPIs, Detailed Sales, Qanz Monthly Group | NewStand User, User |
+----------+-----------------------------------------------+-----------------------------------------------------+---------------------+

Noting that values in columns filters, groups and roles might change.

Is the a postgres select script that can perform that?

Much appreciated!


回答1:


Try this

SELECT
    username,
    string_agg(DISTINCT filters,',') as filters,
    string_agg(DISTINCT groups,',') as roles,
    string_agg(DISTINCT roles,',') as groups
FROM
    table1
GROUP BY
    username;

Demo




回答2:


You can use distinct keyword with aggregation function like array_agg to collect distinct values from each column:

select
    array_agg(distinct username) as username,
    array_agg(distinct filters) as filters,
    array_agg(distinct groups) as groups,
    array_agg(distinct roles) as groups
from YourTable;


来源:https://stackoverflow.com/questions/51249508/from-multiple-rows-to-single-column-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!