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. selecting MAX date records

selecting MAX date records

Scheduled Pinned Locked Moved Database
question
2 Posts 2 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.
  • J Offline
    J Offline
    Joshua Lunsford
    wrote on last edited by
    #1

    What am I doing wrong? select username AS UserID, domainname AS Domain, dt AS Date from testtable gives me: UserID, Domain, Date kt001, c_domain, 2/20/2006 kt001, p_domain, 2/15/2006 kt001, p_domain, 2/19/2006 which is normal. SELECT username AS UserID, MAX(domainname) AS Domain, MAX(dt) AS Date FROM testtable GROUP BY username gives me: UserID, Domain, Date kt001, p_domain, 2/20/2006 which i would want the domain of whatever the MAX date was... any clues?

    M 1 Reply Last reply
    0
    • J Joshua Lunsford

      What am I doing wrong? select username AS UserID, domainname AS Domain, dt AS Date from testtable gives me: UserID, Domain, Date kt001, c_domain, 2/20/2006 kt001, p_domain, 2/15/2006 kt001, p_domain, 2/19/2006 which is normal. SELECT username AS UserID, MAX(domainname) AS Domain, MAX(dt) AS Date FROM testtable GROUP BY username gives me: UserID, Domain, Date kt001, p_domain, 2/20/2006 which i would want the domain of whatever the MAX date was... any clues?

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      I think you may want to handle this as a subquery - something like this:

      Select a.username as User, b.domainname as Domain, a.MaxDT as Date
      From
      (
      Select username, Max(dt) as MaxDT
      From testtable
      Group By username
      ) a INNER JOIN testtable b ON a.username=b.username AND a.MaxDT = b.dt

      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