File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes SQL query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "SQL query" Watch "SQL query" New topic

SQL query

sahidul karim

Joined: Sep 19, 2006
Posts: 28
In one table one coloumn name is "Name". This name coloumn has 20 records. First ten records contain the value as "surender". Next ten records contain the value as "govind". I want the query to display the 1st record as "surender" and 2nd record as "govind" and 3rd record as "surender" 4th record as "govind" . like that means alternatively display the names. please tell me the query.
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
What if you simply query the database with two simple selects:

SELECT name FROM table WHERE name = 'surrender'
SELECT name FROM table WHERE name = 'govind'

This would give you all the records from each case. Now just loop through the ResultSets and interleave them.

You could also just do a simple: SELECT name FROM table and then just use some Java logic on the ResultSet to get the data how you want it.

I don't know of anything in SQL to group the data like that, most of the time I am trying to sort like items together. I never tried alternating, I am not sure if SQL can do this, I will be interested to see what others come up with.

That may not be the best solution, but I think it could work. Maybe someone has some SQL mastery to solve this otherwise(I'm not great at SQL, just know enough to get by).

It might help to have some more background, why do you need to alternate the data? What else is i the table? I'm not sure if I really understand what you are getting here.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33119

Can you describe the problem you are trying to solve a bit more? In particular, if there is something more abstract. The current problem can be solved by hard coding which clearly isn't what you want.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
ryan bohnert

Joined: Feb 27, 2008
Posts: 19
select name from (select rank () over (partition by name order by rownum) seq, name from blow) order by seq, name
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 225

Assalam Alekum,

Can you paste your table structure if you still not got the solution ?

Baseet Ahmed
I agree. Here's the link:
subject: SQL query
It's not a secret anymore!