How to convert row-oriented data to column-oriented data

I have a JDBC ResultSet that has data in a row fashion, e.g. name | value, I want to transpose that data into columns and put it into a csv file.  Can someone help me with the algorithm?  Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
so you have to read ALL columns one by one
and write all values you find separated by , and text by "

so First you run along the names
then you run along the values.

otoh, why bother, and not process the data as you go...
Bernard S.CTOCommented:
If your csv data will be used in Excel, you might decide to do the transposition in Excel itself with a "special copy"
databases can do this using transpose/pivot but can be quite involved for a java developer.

You could do it in java with the help of maps.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Bernard S.CTOCommented:
Rereading your question... JDBC ResultSet that has data in a row fashion, e.g. name | value
Do you mean that you get back several lines for each table-row, each line being name | value? How is the row name given?
mmingfeilamAuthor Commented:
No, one line per record, each record with several fields. The pipes is just for illustration. I guess for now I will have to loop thru the result set with several lists.  Each list will contain all the values for a column.  I will then merge the lists to create the csv string.
Bernard S.CTOCommented:
1 - Could you provide us with a reduced sample, just in case we find some other idea?

2 - Does this comes from some kind of pivot table to want to generate?
Vitor MontalvãoMSSQL Senior EngineerCommented:
Which RDMS are you using?
Can you post some sample data?
Tomas Helgi JohannssonCommented:

You could use OpenCSV ( to export ResultSets to CSV file if that is what you want to do. Then is as simple as this

CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
java.sql.ResultSet myexportedResultSet;
//set myexportedResultSet to the results of your query here
writer.writeAll(myexportedResultSet, includeHeaders);

Open in new window

If you have columns that has Strings in the form "<somename>|<somevalue>" as I understand your question then you can use it like this

ResultSet result = statement.executeQuery("select * from people");
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
     // for each row in the Resultset
     // feed in your array (or convert your data to an array)
     String[] entries = result.getString("yourcolumn").split("|");

Open in new window

     Tomas Helgi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmingfeilamAuthor Commented:
let's say in cassandra DB I have:

Header1 | Header2
val01       | val02
val11       | val 12

I want (headers stripped):
val01 | val11
val02 | val12
Tomas Helgi JohannssonCommented:

You can export the whole ResultSet in one go like this
writer.writeAll(myResultSet, includeHeaders);
writer.writeAll(myResultSet, excludeHeaders);

But in the demo I showed in my first comment you only put out the data not the headers.

     Tomas Helgi
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.