DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  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



Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links