• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SQL query

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 225
Spring Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assalam Alekum,

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




Regards
Baseet Ahmed
reply
    Bookmark Topic Watch Topic
  • New Topic