Reputation: 14779
I've been given a bunch of contacts in csv format like so:
companyID, companyName, contactId, firstName, lastName, email
And asked to merge all the contacts from a single company into a single row like so
companyID, companyName, contactId, firstName, lastName, email, companyName, contactId, firstName, lastName, email...
As to why they want the data like this, I have no idea.
I'm not tied to any particular technology as long as it's freely available and I get the right result. How would you achieve this?
So far I tried importing into a postgres table and attempting various joins and recursive queries but I can't quite come up with the right syntax.
Upvotes: 1
Views: 971
Reputation: 31270
If you have access to Unix/Linux or CygWin on Windows, you could use
sort csvFileName | awk -F, 'BEGIN {last="";} {if (last == $1) { printf ","; } else { printf "\n"; }; printf $0; last =$1; }'
This would repeat the CompanyID each times but you can alter the printf 0$ to output columns other than $1 or you could post process to remove those columns.
Upvotes: 1
Reputation: 38348
Here is a potential solution:
Map<Company, List<Contact>>
to map contacts to a company.List<Contact>
for each company.OpenCSV might be helpful.
If you don't find an open source CSV reader, you can split the line based on comma (,) and in the Company and Contact classes, just implement something like public String toCSV()
class to output the object as a CSV.
Upvotes: 0