Retrieve Dates (and other data) From Different Rows

  • Hi,

    I need to retrieve dates (and other fields) from different rows. How do I accomplish this?

    Data looks like:

    plan_numcall_numpackage_numcompleted_datenext_scheduled_datejob_id
    1234561120130112201512311000000
    1234562120140224201603171000000
    1234563120141213201702021000000
    1234564120150917201711211000000
    1234565100000000201811211000000
    7890121120150713201512291000000
    7890122120160821201607081000000
    7890123100000000201708241000000

    The user will type in the job_id. I need to return plan_num, package_num, the most recent completed date and the scheduled date from the row following the most recent completed date. So, in this case, the user will type in job_id 1000000 and I need to return all of the following: 

    123456 1 20150917 20181121
    789012 1 20160821 20170824

    Thank you!

  • Please don't post multiple threads for the same question.
    No replies here. Replies to https://www.sqlservercentral.com/Forums/1852028/Retrieve-Dates-from-Different-Rows

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I recommend you post your data according to the methods depicted at the first link in my signature line below under "Helpful Links".  People will produce a coded response for you that has actually been tested.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply