#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    How to select earliest record?


    hello everyone, im trying to get the earliest record. data is below

    note_id/ doc_received_date/ bankruptcy_date/ sp_recorded_date
    2332/ 20090106<----/ 20081219/ 20090106
    2332/ 20090323/ 20081219/ 20090323
    2332/ 20090413/ 20081219/ 20090413
    2332/ 20090507/ 20081219/ 20090507


    because the bankruptcy_date date are all equal i would need to pull one record with the earliest date from the doc_received_date. The date with the arrow is the record i want. So basically i would like to show this result

    note_id/ doc_received_date/ bankruptcy_date/ sp_recorded_date
    2332 20090106 20081219 20090106

    Hope this made sense.. Thanks for you ur help
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by Biznez
    hello everyone, im trying to get the earliest record. data is below

    note_id/ doc_received_date/ bankruptcy_date/ sp_recorded_date
    2332/ 20090106<----/ 20081219/ 20090106
    2332/ 20090323/ 20081219/ 20090323
    2332/ 20090413/ 20081219/ 20090413
    2332/ 20090507/ 20081219/ 20090507


    because the bankruptcy_date date are all equal i would need to pull one record with the earliest date from the doc_received_date. The date with the arrow is the record i want. So basically i would like to show this result

    note_id/ doc_received_date/ bankruptcy_date/ sp_recorded_date
    2332 20090106 20081219 20090106

    Hope this made sense.. Thanks for you ur help
    Hello,

    how about something like SELECT TOP 1 * FROM [tableName] ORDER BY doc_received_date DESC
  4. #3
  5. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,027
    Rep Power
    4210
    Originally Posted by LetThereBeByte
    how about something like SELECT TOP 1 * FROM [tableName] ORDER BY doc_received_date DESC
    yes, if "something like" is this --
    Code:
    SELECT TOP 1 *
      FROM daTable
    ORDER
        BY bankruptcy_date
         , doc_received_date
    note that DESC is not correct in this probelm


    there's also this approach --
    Code:
    SELECT *
      FROM daTable
     WHERE doc_received_date =
           ( SELECT MIN(doc_received_date)
               FROM daTable
              WHERE bankruptcy_date =
                    ( SELECT MIN(bankruptcy_date)
                        FROM daTable ) )
    r937.com | rudy.ca
    please visit Simply SQL and buy my book

IMN logo majestic logo threadwatch logo seochat tools logo