'Query to return column values as comma separated - postgresql
I am using materialized view in my application.
The query is as follows
SELECT
DISTINCT n.order_id,
CASE
WHEN n.customer_billing = TRUE THEN 'AR (Customer Billing)'
WHEN n.commander = TRUE THEN 'AP (Commander)'
WHEN n.agent = TRUE THEN 'AP (Agent)'
ELSE NULL
END AS finance
FROM
pt.notes n
WHERE
n.order_id = 891090 AND
(n.customer_billing = TRUE or n.commander = TRUE or n.agent = TRUE)
This produces the following output
Instead of two records, the output of finance column should be comma separated values.
That is the output should be
AP (Agent), AR (Customer Billing)
I know aggregate functions can be used here, but not sure how to use when the query has switch cases and all.
Any idea on how to achieve this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

