Consulting

Results 1 to 2 of 2

Thread: SQL Add 2nd Most Recent Date From Another Table

  1. #1
    VBAX Regular
    Joined
    May 2015
    Posts
    87
    Location

    SQL Add 2nd Most Recent Date From Another Table

    SELECT A.FILE_NO, DATE() - 1 AS Yesterday, A.BA_INT_RATE, (SELECT MAX(sys.SystemDate) FROM SystemDate AS sys) AS systemDate
    FROM Claim AS A
    WHERE A.STATUS_CODE = "OPN";

    The part of the code above that's red selects the most recent record from the system date table.
    I need to add a new field which displays the 2nd most recent date from the table.

    There are no duplicate dates but the dates do skip around so I can't simply use (systemDate - 1) AS lastSystemDate

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You gonna like this, not a lot

    SELECT A.FILE_NO
          ,DATE() - 1 AS Yesterday
    	  ,A.BA_INT_RATE
    	  ,(SELECT MAX(SystemDate) FROM SystemDate) AS systemDate 
    	  ,(SELECT MAX(SystemDate) FROM SystemDate WHERE SystemDate < (SELECT MAX(SystemDate) FROM SystemDate)) AS systemDate1 
    FROM Claim AS A 
    WHERE A.STATUS_CODE = "OPN";
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •