get max and min date corresponding values

  • I have two tables.

    Emp:

    id | Deptno | sdate | edate | locid

    1 | 10 |2014-09-19 |2009-09-25 | 100

    1 | 10 |2014-11-06 |2009-11-06 | 100

    1 | 10 |2014-12-11 |2014-12-14 | 100

    1 | 10 |2009-07-01 |2009-07-20 | 100

    2 | 20 |2007-07-11 |2007-07-17 | 104

    Empl1:

    Id | Deptno | edate | refid | refid1

    1 | 10 | 2014-11-06 | 201 | 204

    1 | 10 | 2014-12-13 | 204 | 202

    1 | 10 | 2014-12-12 | 207 | 124

    1 | 10 | 2003-05-04 | 105 | 103

    2 | 20 | 2007-07-14 | 102 | 106

    I am trying to combine these two tables into the output below:

    Id | Deptno | locid | current_refid | current_refid1 | last_refid | last_refid1

    1 | 10 | 101 | 201 | 204 | 204 |202

    2 | 20 | 104 | 102 | 106 | 102 |106

    The tables share the common columns id and deptno. To get the above result, the emp1.edate must be between emp.sdate and emp.edate.

    If that condition is met, we need to retrieve the refid and refid1 values corresponding to the lowest edate as last_refid and last_refid1. Then we need the refid and refid1 values corresponding to the emp1.edate between emp.stdate and emp.edate to be retrieved as the current_refid and current_refid1.

    I tried to do this using the following query, but it does not give the expected result:

    select a.id,a.deptno,a.locid,b.refid,b.refid1

    from

    emp b

    join

    (

    select * ,row_number()over (partition by id ,deptno order by edate )as rn

    from emp1

    ) a

    where b.edate<=a.sdate and b.edate<=a.edate

    please tell me query how to achive this task in sql server

  • asranantha (8/26/2015)


    I have two tables.

    Emp:

    id | Deptno | sdate | edate | locid

    1 | 10 |2014-09-19 |2009-09-25 | 100

    1 | 10 |2014-11-06 |2009-11-06 | 100

    1 | 10 |2014-12-11 |2014-12-14 | 100

    1 | 10 |2009-07-01 |2009-07-20 | 100

    2 | 20 |2007-07-11 |2007-07-17 | 104

    Empl1:

    Id | Deptno | edate | refid | refid1

    1 | 10 | 2014-11-06 | 201 | 204

    1 | 10 | 2014-12-13 | 204 | 202

    1 | 10 | 2014-12-12 | 207 | 124

    1 | 10 | 2003-05-04 | 105 | 103

    2 | 20 | 2007-07-14 | 102 | 106

    I am trying to combine these two tables into the output below:

    Id | Deptno | locid | current_refid | current_refid1 | last_refid | last_refid1

    1 | 10 | 101 | 201 | 204 | 204 |202

    2 | 20 | 104 | 102 | 106 | 102 |106

    The tables share the common columns id and deptno. To get the above result, the emp1.edate must be between emp.sdate and emp.edate.

    If that condition is met, we need to retrieve the refid and refid1 values corresponding to the lowest edate as last_refid and last_refid1. Then we need the refid and refid1 values corresponding to the emp1.edate between emp.stdate and emp.edate to be retrieved as the current_refid and current_refid1.

    I tried to do this using the following query, but it does not give the expected result:

    select a.id,a.deptno,a.locid,b.refid,b.refid1

    from

    emp b

    join

    (

    select * ,row_number()over (partition by id ,deptno order by edate )as rn

    from emp1

    ) a

    where b.edate<=a.sdate and b.edate<=a.edate

    please tell me query how to achive this task in sql server

    Please read and heed the instructions for how to post a question found by following the first link in my signature. This really isn't too difficult for a query but deciphering your table and data is.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

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

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