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.
LVL 1
mmingfeilamAsked:
Who is Participating?
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...
0
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"
0
gurpsbassiCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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?
0
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.
1
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?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Which RDMS are you using?
Can you post some sample data?
0
Tomas Helgi JohannssonCommented:
Hi!

You could use OpenCSV (http://opencsv.sourceforge.net/) 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');
     while(result.next){
     // for each row in the Resultset
     // feed in your array (or convert your data to an array)
     String[] entries = result.getString("yourcolumn").split("|");
     writer.writeNext(entries);
     }
     writer.close();

Open in new window


Regards,
     Tomas Helgi
0

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
0
Tomas Helgi JohannssonCommented:
Hi!

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

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

Regards,
     Tomas Helgi
0
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
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.