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 rows by earliest date

Selecting rows by earliest date

Scheduled Pinned Locked Moved Database
saleshelpquestion
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.
  • S Offline
    S Offline
    Stefan R
    wrote on last edited by
    #1

    I have a table "tbl_order" with to the fields "customer_number" and "date". there can be multiple rows with the same customer_number, but the date fields are allways unique. these or potential rows in my table: cust_no | date | item --------------------------- 21 2006-01-06 shovel 21 2006-03-05 axe 21 2006-05-05 widget 45 2006-05-01 fish tank 45 2006-05-02 ... For every cust_no i want to retreive only the row with the earliest date. How can this be done? what i want is this: ------------------------ 21 2006-01-06 shovel 45 2006-05-01 fish tank Any help would be greatly appreciated / Stefan

    C 1 Reply Last reply
    0
    • S Stefan R

      I have a table "tbl_order" with to the fields "customer_number" and "date". there can be multiple rows with the same customer_number, but the date fields are allways unique. these or potential rows in my table: cust_no | date | item --------------------------- 21 2006-01-06 shovel 21 2006-03-05 axe 21 2006-05-05 widget 45 2006-05-01 fish tank 45 2006-05-02 ... For every cust_no i want to retreive only the row with the earliest date. How can this be done? what i want is this: ------------------------ 21 2006-01-06 shovel 45 2006-05-01 fish tank Any help would be greatly appreciated / Stefan

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      SELECT q.cust_no, earliest_date, item
      FROM tbl_order as t
      INNER JOIN
      (
      SELECT cust_no, MIN(date) as earliest_date
      FROM tbl_order
      GROUP BY cust_no
      ) AS q ON t.cust_no = q.cust_no AND t.date = q.earliest_date


      Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

      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