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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Query Question

SQL Query Question

Scheduled Pinned Locked Moved Database
databasehelpquestion
4 Posts 3 Posters 0 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.
  • M Offline
    M Offline
    matt cole
    wrote on last edited by
    #1

    Hi, I have a question about a sql query I need to do. I have a table with 3 fields, userID, entryDate and value. My query will have a date parameter called targetDate. What I was hoping to do was retrieve a single row for each user where the entryDate is the most recent entry for that user prior to the targetDate parameter. If there are no entries prior the the targetDate for the user, no rows will be returned for that user, if they have multiple entries with entryDate < targetDate, obviously only the most recent will be returned. If anyone was able to help me with the SQL for this, that'd be great. Thanks, Matt

    J 1 Reply Last reply
    0
    • M matt cole

      Hi, I have a question about a sql query I need to do. I have a table with 3 fields, userID, entryDate and value. My query will have a date parameter called targetDate. What I was hoping to do was retrieve a single row for each user where the entryDate is the most recent entry for that user prior to the targetDate parameter. If there are no entries prior the the targetDate for the user, no rows will be returned for that user, if they have multiple entries with entryDate < targetDate, obviously only the most recent will be returned. If anyone was able to help me with the SQL for this, that'd be great. Thanks, Matt

      J Offline
      J Offline
      Jon Hulatt
      wrote on last edited by
      #2

      You have do do this with a two pronged approach. Use can use max() and group by clause to get the newest date (only) for each customer, then you can join back on the table to get the rest of the data:-

      select yourTable.* from (
      select userID, max(entryDate) as entryDate
      from yourTable
      group by userid
      ) k
      inner join yourTable
      on yourTable.userId = k.userId and yourTable.entryDate = k.entryDate

      using System.Beer;

      A M 2 Replies Last reply
      0
      • J Jon Hulatt

        You have do do this with a two pronged approach. Use can use max() and group by clause to get the newest date (only) for each customer, then you can join back on the table to get the rest of the data:-

        select yourTable.* from (
        select userID, max(entryDate) as entryDate
        from yourTable
        group by userid
        ) k
        inner join yourTable
        on yourTable.userId = k.userId and yourTable.entryDate = k.entryDate

        using System.Beer;

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Thanks Jon! That works perfectly :)

        1 Reply Last reply
        0
        • J Jon Hulatt

          You have do do this with a two pronged approach. Use can use max() and group by clause to get the newest date (only) for each customer, then you can join back on the table to get the rest of the data:-

          select yourTable.* from (
          select userID, max(entryDate) as entryDate
          from yourTable
          group by userid
          ) k
          inner join yourTable
          on yourTable.userId = k.userId and yourTable.entryDate = k.entryDate

          using System.Beer;

          M Offline
          M Offline
          matt cole
          wrote on last edited by
          #4

          Thanks Jon, That works perfectly :)

          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