0

I am working on Postgres SQL and having below join query, when I execute this query I get two or 3 records for each employee, because each employee has 3 different types of email address 1) Home Address 2) Office address 3) social address.

select * from root.employee c
full outer join root.employee_email ce
on c.employee_id = ce.employee_id
order by c.employee_id limit 1000 offset 0;

What I want is that employee_email.email column to give the comma separated value in the output, how can we do that ?

Note: I've almost 1 million records in DB and will use Spring Batch to migrate data from Postgres to MongoDB. The same I would need to do for Phone

1 Answer 1

1

Aggregate by employee and use string_agg:

select
    c.employee_id,         -- or just c.* assuming employee_id is a PK
    string_agg(ce.email, ',') as emails
from root.employee c
full outer join root.employee_email ce
    on c.employee_id = ce.employee_id
group by
    c.employee_id
order by
    c.employee_id
limit 1000
offset 0;
Sign up to request clarification or add additional context in comments.

7 Comments

What would I need to do get all fields from both the tables ?
Is employee.employee_id the primary key column?
Right, but I need all the field to be get mapped it from result set and creat object in Spring Batch
Yes, employee.employee_id is Primary Key column and its UUID
@JeffCook Then you should be able to select all columns in the employee table, q.v. the updated answer. As for the other table, we would have to aggregate its columns in some way.
|

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.