my output is look like:
id order | status | item | supplier | confirm date | customer | Manager |
---|---|---|---|---|---|---|
555222 | approved | 12421 | Hawi | 12/02/2023 | 0122 | George |
555222 | approved | 12421 | Hawi | 12/02/2023 | 0122 | Michael |
555222 | approved | 12421 | Hawi | 12/02/2023 | 0122 | Tom |
543677 | approved | 85332 | Mint | 27/01/2023 | 0324 | John |
200013 | approved | 90210 | Mint | 27/01/2023 | 0714 | Brad |
111111 | approved | 80765 | Sweet | 11/01/2023 | 0122 | George |
111111 | approved | 80765 | Sweet | 11/01/2023 | 0122 | Michael |
111111 | approved | 80765 | Sweet | 11/01/2023 | 0122 | Tom |
I was trying to merge all the managers of customer 0122 in one row by using group_concat and seperate, but i got struggle with the "group by" because i have more fields in the select (like 78 columns in the output).
i want to know if there is any way my query can use the "where" clause for searching all my customers if they have more then 1 manager and if there is, the query will do something to merge them?
***the manager column and the customer column are coming from different tables.
for example the output i would like to get:
id order | status | item | supplier | confirm date | customer | Manager |
---|---|---|---|---|---|---|
555222 | approved | 12421 | Hawi | 12/02/2023 | 0122 | George, Michael, Tom |
543677 | approved | 85332 | Mint | 27/01/2023 | 0324 | John |
200013 | approved | 90210 | Mint | 27/01/2023 | 0714 | Brad |
111111 | approved | 80765 | Sweet | 11/01/2023 | 0122 | George, Michael, Tom |
2
Answers
use group_concat() by using the group columns and take it 1st cte then in another select the rest of yours 72 columns and join with the 1st cte
so sudo code will be like below
Presumably your are joining to customer and manager data somehow. So, you could
GROUP BY
the customer in a subquery before joining the subquery to your orders.This is a crude example, assuming that orders, customers and customer_managers come from different tables:
If you have a vast number of customers and do not want to pre-aggregate the data for all of them, you could make the JOIN LATERAL:
Here’s a db<>fiddle
Hopefully, this serves as an example of how to apply it to your data.