1

Given is a Postgres table like this

nummer  vorname    name        cash
------|-----------|-----------|-----
1       paul       smith       500
2       andy       london      700
2       mike       dover       700
3       clara      winchester  200

To query this table my sql looks like this right know:

SELECT 
  nummer,
  vorname,
  name,  
  cash as total
FROM 
  data.myTable
GROUP BY
  nummer, 
  name,
  vorname,
  cash
ORDER BY
  nummer; 

Is it possible to concat the two rows where nummer is the same (in this case 2). Means my output should look like this (cash will also have the same values if the numbers are equal):

nummer  vorname    name        cash
------|-----------|-----------|-----
1       paul       smith       500
2       andy       london      700
        mike       dover       
3       clara      winchester  200

2 Answers 2

10

Use GROUP BY and the aggregate functioin string_agg():

SELECT nummer
      ,string_agg(vorname, E'\n') AS vorname
      ,string_agg(name, E'\n') AS name
      ,cash
FROM   mytable
GROUP  BY nummer, cash
ORDER  BY nummer, cash;

I added cash to the GROUP BY to get to the original value and safeguard against the case where it would be different for the same nummer.

As to your comment:

is it possible to unique the query also by names. means if 2|andy london|700 is listed twice one should be removed.

SELECT nummer
      ,string_agg(vorname, E'\n') AS vorname
      ,string_agg(name, E'\n') AS name
      ,cash
FROM  (
   SELECT DISTINCT
          nummer, vorname, name, cash
   FROM   mytable
   ) AS m
GROUP  BY nummer, cash
ORDER  BY nummer, cash;
Sign up to request clarification or add additional context in comments.

1 Comment

@tBook: Note that I removed DISTINCT from the aggregate function. Was an oversight. You could use that to remove dupes per column (which you did not specify).
1
SELECT nummer, 
       array_to_string(array_agg(vorname), E'\n'), 
       array_to_string(array_agg(name), E'\n'),
       cash
  FROM mytable
  group by nummer, cash;

That should do it.

4 Comments

thanks a million this works great, i recongnized one more issue. is it possible to unique the query also by names. means if 2|andy london|700 is listed twice one should be removed. -> in other word how can i add an distinct on name, vorname, cash ? thanks tony
fixed it. As to get rid of the duplicate names, that's a tough one. you have some issues here regarding data dependencies. Working on this side. one sec.
@Chris, thanks for your input. The cash in the "group by" is a good step, this works. regarding the names. in this case it should look like the second code example in my first post. This works so far execpt the duplicate names. in other words in those cases nummer and cash are always the same. nummer and cash should be merged (works!), names should be merged and duplicated should be kicked out. possible? my other idea word be to save all data in a php array and do something like array unique … kind regards, tony
@Chris: got it with a small adaption from erwins snippet, I would post it but not sure how to do it correctly on stackoverflow. thanks both of YOU a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.