Request the last "n" rows


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Request the last "n" rows

  1. #1
    PhilipL Guest

    Request the last "n" rows


    I know how to use the Top function but how can I retieve the last "n" records
    from a table using "n" as a variable. I tried using a negative number but
    that didn`t work.

    Any advice or link is appreciated.

    PhilipL

  2. #2
    PhilipL Guest

    Re: Request the last "n" rows


    "PhilipL" <aplatfl@aol.com> wrote:
    >
    >I know how to use the Top function but how can I retieve the last "n" records
    >from a table using "n" as a variable. I tried using a negative number but
    >that didn`t work.
    >
    >Any advice or link is appreciated.
    >
    >PhilipL


    I am using sql server 7 with vb.

  3. #3
    Chris Hylton Guest

    Re: Request the last "n" rows


    order by the field you want to pick by (descending order) and still use top
    N in your select statement...

    Chris

    "PhilipL" <aplatfl@aol.com> wrote:
    >
    >"PhilipL" <aplatfl@aol.com> wrote:
    >>
    >>I know how to use the Top function but how can I retieve the last "n" records
    >>from a table using "n" as a variable. I tried using a negative number but
    >>that didn`t work.
    >>
    >>Any advice or link is appreciated.
    >>
    >>PhilipL

    >
    >I am using sql server 7 with vb.



  4. #4
    srr Guest

    Re: Request the last "n" rows


    Use
    DECLARE @i INTEGER
    SET @i = 10

    SET ROWCOUNT @i

    SELECT * FROM tbl ORDER BY DateField DESC
    or

    SELECT * FROM tbl
    WHERE PK IN(SELECT * FROM tbl ORDER BY dateField DESC)
    ORDER BY DateField ASC

    SET ROWCOUNT 0

    Play with this I think this is what you need.

    Scott

    "PhilipL" <aplatfl@aol.com> wrote:
    >
    >I know how to use the Top function but how can I retieve the last "n" records
    >from a table using "n" as a variable. I tried using a negative number but
    >that didn`t work.
    >
    >Any advice or link is appreciated.
    >
    >PhilipL



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center