• 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

Question on Cursors

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is an ORACLE SQL question, rather than a JDBC question.
Suppose i have a table 'Table1' with the following records:

Fund :Col2: SeqNo: Col4
========================
1 1 1 x
1 1 2 y
2 1 5 y
2 1 6 x
1 0 3 p
2 0 4 y

The Query that would return this resultset in the order given above is:
Select * from table1 order by Col2 desc,SeqNo asc.
My question is how do i define a Cursor in my stored proc such that i get the last 2 (n in general) rows in the example specific above. i.e How do i define a cursor such that i get the records with the lowest Col2 (for a Fund) and the highest SeqNo for this lowest Col2 (for the fund).
Please note that the record for a given Fund with the lowest Col2 may not have the highest SeqNo for the Fund (as in Record#6 and Record#4 in the example).

Please give the Query for the cursor.
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oracle has some excellent analytical functions which solves your question.

Here is the query for your dataset. But you can change it to get any desired results. For more information analytical functions take a look Oracle Data warehousing guide



The above query gives least col2 record (and max seq within the least col2) for each fund. If you need the least col2 across fund you need to change the over() clause.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic