• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL query

 
sahidul karim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahidul,
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.
 
ryan bohnert
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
select name from (select rank () over (partition by name order by rownum) seq, name from blow) order by seq, name
 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assalam Alekum,

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




Regards
Baseet Ahmed
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic