Skip to content

Tarik Billa

  • Web Development
    • html
    • vue.js
    • laravel
    • css
    • javascript
    • jquery
    • node.js
    • php
    • asp.net
  • Programming
    • python
    • java
    • c
    • c++
    • c#
  • git
  • android

array-agg

How to aggregate two PostgreSQL columns to an array separated by brackets

November 26, 2023 by Tarik

Create an array from the two columns, the aggregate the array: select id, array_agg(array[x,y]) from the_table group by id; Note that the default text representation of arrays uses curly braces ( {..}) not square brackets ([..])

Categories sql Tags array-agg, arrays, concatenation, postgresql, sql Leave a comment

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

January 22, 2023 by Tarik

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

Categories sql Tags array-agg, postgresql, select, sql Leave a comment

PostgreSQL array_agg order

November 28, 2022 by Tarik

Use an ORDER BY, like this example from the manual: SELECT array_agg(a ORDER BY b DESC) FROM table;

Categories postgresql Tags array-agg, postgresql Leave a comment

Tarik Billa

Software Engineer
tarikbilla@gmail.com
+8801884414000
  • Reuse a hash in YAMLApril 17, 2024
  • Dockerfile: how to redirect the output of a RUN command to a variable?April 16, 2024
  • How to cd to a directory with spaces in the directory name?April 16, 2024
  • Maximum MIME type length when storing the type in a databaseApril 16, 2024
  • What is the difference between Unit, Integration, Regression and Acceptance Testing?April 16, 2024
© 2026 Tarik Billa