Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to Select the last N records based on date?

How to Select the last N records based on date?

Scheduled Pinned Locked Moved Database
questiondatabasetutorial
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shapper
    wrote on last edited by
    #1

    Hello, I need to load the last N records from a table. The last N records are determined by date. And I want N to be a parameter in the Query. How can I do this? I allready have a Query. The only thing I am missing is that part. SELECT web_News.ArticleDate, web_NewsLocalized.ArticleTitle, web_NewsLocalized.ArticleText FROM web_News INNER JOIN web_NewsLocalized ON web_News.ArticleId=web_NewsLocalized.ArticleId WHERE ((web_NewsLocalized.ArticleCulture)=[@Culture]); Thanks, Miguel

    N 1 Reply Last reply
    0
    • S shapper

      Hello, I need to load the last N records from a table. The last N records are determined by date. And I want N to be a parameter in the Query. How can I do this? I allready have a Query. The only thing I am missing is that part. SELECT web_News.ArticleDate, web_NewsLocalized.ArticleTitle, web_NewsLocalized.ArticleText FROM web_News INNER JOIN web_NewsLocalized ON web_News.ArticleId=web_NewsLocalized.ArticleId WHERE ((web_NewsLocalized.ArticleCulture)=[@Culture]); Thanks, Miguel

      N Offline
      N Offline
      nguyenvhn
      wrote on last edited by
      #2

      If you are using SQL Server 2005, there is a simple way: SELECT TOP(@N) web_News.ArticleDate, web_NewsLocalized.ArticleTitle, web_NewsLocalized.ArticleText FROM web_News INNER JOIN web_NewsLocalized ON web_News.ArticleId=web_NewsLocalized.ArticleId WHERE ((web_NewsLocalized.ArticleCulture)=[@Culture]) ORDER BY web_News.ArticleDate DESC;

      S 1 Reply Last reply
      0
      • N nguyenvhn

        If you are using SQL Server 2005, there is a simple way: SELECT TOP(@N) web_News.ArticleDate, web_NewsLocalized.ArticleTitle, web_NewsLocalized.ArticleText FROM web_News INNER JOIN web_NewsLocalized ON web_News.ArticleId=web_NewsLocalized.ArticleId WHERE ((web_NewsLocalized.ArticleCulture)=[@Culture]) ORDER BY web_News.ArticleDate DESC;

        S Offline
        S Offline
        shapper
        wrote on last edited by
        #3

        Hi, I am using Access. The top (@N) is not working. I get an error when trying to save the Query. I have been looking in internet but I can't find the solution. I also tried [@N] but no luck. Any idea? Thanks, Miguel

        N 1 Reply Last reply
        0
        • S shapper

          Hi, I am using Access. The top (@N) is not working. I get an error when trying to save the Query. I have been looking in internet but I can't find the solution. I also tried [@N] but no luck. Any idea? Thanks, Miguel

          N Offline
          N Offline
          nguyenvhn
          wrote on last edited by
          #4

          SELECT web_News.ArticleDate, web_NewsLocalized.ArticleTitle, web_NewsLocalized.ArticleText, (SELECT COUNT(*) FROM web_News AS B WHERE B.ArticleDate>=A.ArticleDate) AS RowNumber FROM web_News AS A INNER JOIN web_NewsLocalized ON web_News.ArticleId=web_NewsLocalized.ArticleId WHERE ((web_NewsLocalized.ArticleCulture)=[@Culture] AND RowNumber<=@N) ORDER BY web_News.ArticleDate DESC; I am not sure wheather it works, I've not tested yet, but just try it anyway. The performance will be a big problem

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups