Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    15

    Unanswered: First and last record id in a select query

    Is there a function or a way in sybase to get the first and last record id number in a select query?

    example: SELECT FIRST(tr_id).....FROM...

    Thank you in advance for your help, is much appreciated it!

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    SELECT MIN(tr_id) ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2006
    Posts
    15
    That helps....
    but let me complicate it a little more:....I need to get the min(tr_id) and the max(tr_id) for every 500,000 records.
    Can this be done?

    Example:

    set rowcount 500,000
    SELECT MIN(TR_ID) AS mintrid, MAX(TR_ID) AS maxtrid
    FROM PERSON
    WHERE pr_id > 0
    ORDER BY pr_id;

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    no, sorry, doesn't make sense to me, maybe someone else?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Sep 2003
    Location
    Switzerland
    Posts
    443
    Quote Originally Posted by Malachi4
    That helps....
    but let me complicate it a little more:....I need to get the min(tr_id) and the max(tr_id) for every 500,000 records.
    Can this be done?

    Example:

    set rowcount 500,000
    SELECT MIN(TR_ID) AS mintrid, MAX(TR_ID) AS maxtrid
    FROM PERSON
    WHERE pr_id > 0
    ORDER BY pr_id;
    set rowcount wouldnt work.

    Not sure why you want want to do it for every 500k, but if you do need to do it, you need to have a condition where you can identify the 500k rows uniquely.

    For eg., maybe you have a sequential primary key.

    you can say, WHERE myseqid between 1 and 500000 and so on... but dont think it can be done in one single sql

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •