How to remove duplicates, which are generated with array_agg postgres function

后端 未结 1 1522
野性不改
野性不改 2021-02-03 17:14

Does anyone an idea how to rewrite following SQL query to generate results, that would contains only one occurrence of name? (results grouped by user).

The query

1条回答
  •  后悔当初
    2021-02-03 17:34

    You can use the distinct keyword inside array_agg:

    SELECT ARRAY_TO_STRING(ARRAY_AGG(DISTINCT CONCAT(u.firstname, ' ', u.lastname)), ', ')
    FROM log_has_item logitem
      INNER JOIN log log ON log.id = logitem.log_id
      INNER JOIN worker u ON log.worker_id = u.id
    WHERE logitem.company_id = 1
    

    SQLFiddle with this example

    0 讨论(0)
提交回复
热议问题