Rehire - Current and last employment

I have a employee table like this
Sample data
WITH emp AS
SELECT 1 PK_ID , '10' EMP_NO , TO_DATE('01-JAN-2012')  HIRE_DATE , TO_DATE('01-APR-2012')  LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 2 PK_ID , '10' EMP_NO , TO_DATE('01-JUN-2012')  HIRE_DATE , NULL LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 3 PK_ID , '20' EMP_NO , TO_DATE('01-FEB-2012')  HIRE_DATE , null LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 4 PK_ID , '30' EMP_NO , TO_DATE('01-AUG-2011')  HIRE_DATE , TO_DATE('01-SEP-2011') LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 5 PK_ID , '30' EMP_NO , TO_DATE('01-OCT-2011')  HIRE_DATE , TO_DATE('01-NOV-2011') LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 6 PK_ID , '30' EMP_NO , TO_DATE('01-DEC-2011')  HIRE_DATE , NULL  LAST_WORKING_DATE FROM DUAL
);I need a query to fetch the current and previous employement only if the employee is rehired. Current Employment can be identified if last_working_date is null.
So from above emp_no 10 and 30 should return pk_id 1 and 2.. where 1 is previous employment and 2 is current employement... Emp_no 20 should not be fetched since he is not been rehired in the company..
Emp no 30 should be fetched only pk_id 5 and 6... 6 is the current employment and 5 is previous employment
Thanks
Vinoth.

Try this
SELECT pk_id, emp_no,CURRENT_HIRE_DATE , PREVIOUS_HIRE_DATE, CURRENT_LAST_WORKING_DATE, PREVIOUS_LAST_WORKING_DATE
FROM (
SELECT pk_id, emp_no,HIRE_DATE "CURRENT_HIRE_DATE",lead(HIRE_DATE,1,NULL) over(PARTITION BY emp_no ORDER BY DMY_ORDER) "PREVIOUS_HIRE_DATE"
       , LAST_WORKING_DATE "CURRENT_LAST_WORKING_DATE",  lead(LAST_WORKING_DATE,1,NULL) over(PARTITION BY emp_no ORDER BY DMY_ORDER) "PREVIOUS_LAST_WORKING_DATE"
       ,row_number() over (PARTITION BY emp_no ORDER BY hire_date DESC) "DMY_SLCT"
FROM
SELECT pk_id, emp_no,HIRE_DATE,LAST_WORKING_DATE, COUNT(emp_no) over (PARTITION BY emp_no) "CNT",
       row_number() over (PARTITION BY emp_no ORDER BY hire_date DESC) "DMY_ORDER"
FROM 
SELECT 1 PK_ID , '10' EMP_NO , TO_DATE('01-JAN-2012')  HIRE_DATE , TO_DATE('01-APR-2012')  LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 2 PK_ID , '10' EMP_NO , TO_DATE('01-JUN-2012')  HIRE_DATE , NULL LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 3 PK_ID , '20' EMP_NO , TO_DATE('01-FEB-2012')  HIRE_DATE , null LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 4 PK_ID , '30' EMP_NO , TO_DATE('01-AUG-2011')  HIRE_DATE , TO_DATE('01-SEP-2011') LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 5 PK_ID , '30' EMP_NO , TO_DATE('01-OCT-2011')  HIRE_DATE , TO_DATE('01-NOV-2011') LAST_WORKING_DATE FROM DUAL
UNION ALL
SELECT 6 PK_ID , '30' EMP_NO , TO_DATE('01-DEC-2011')  HIRE_DATE , NULL  LAST_WORKING_DATE FROM DUAL)
WHERE CNT >=2 AND DMY_ORDER IN (1,2))
WHERE DMY_SLCT=1;
Output..
PK_ID     EMP_NO     CURRENT_HIRE_DATE     PREVIOUS_HIRE_DATE     CURRENT_LAST_WORKING_DATE     PREVIOUS_LAST_WORKING_DATE
2     10     6/1/2012     1/1/2012          4/1/2012
6     30     12/1/2011     10/1/2011          11/1/2011
Hope its fullfill your requirement.

Similar Messages

  • Current and Last PO Calculation

    Hello All,
    0PUR_C01 is my InfoProvider. I also have data in 0PUR_O01, which is an DSO.
    I need to build a report giving Current and Last PO details for a Material (independent of vendor).
    The user will input only single date. The system should check which is the latest and latest-1 PO using that input date.
    I am looking for a customer exit which can solve this.
    Thanks,
    Mainak

    Hi Mainak,
    I do not have the exact code as per your requirements.This is sample code for Quantity field with the document date field ( date should be restricted between system date and system date+112 days).
    WHEN 'ZPRDATE1'.
    IF i_step = 2.
    READ TABLE I_T_VAR_RANGE INTO LOC_VAR_RANGE
    WITH KEY VNAM = 'ZPRDATE'.
    v_date1 = LOC_VAR_RANGE-LOW.
    ddate1 = sy-datum.
    l_s_range-sign = 'I'.
    *l_s_range-opt = 'EQ'.
    l_s_range-opt = 'BT'.
    l_s_range-low = ddate1.
    l_s_range-high = sy-datum + 112. (Check syntax)
    Append l_s_range to E_T_RANGE.
    ENDIF.
    If  your variable  want to be defined as "Ready for Input" ?
    pls refer this.
    The RSR00001 enhancement ("BW: Enhancements for global variables in reporting") is called repeatedly while the report is being executed. In this case, the I_STEP parameter specifies when the enhancement is being called.
    The following values are valid for I_STEP:
    I_STEP = 1
    Call directly before the variable is entered
    I_STEP = 2
    Call directly after the variable is entered. This step is activated only if the same variable could not be filled for I_STEP = 1 and it is not ready for input.
    I_STEP = 3
    In this call, you can check the values of the variables. Activating an exception (RAISE) causes the variable screen to appear again. I_STEP = 2 is also then executed once more.
    I_STEP = 0
    The enhancement is not called from the variable screen. The call can come from the authorization check or from the monitor.
    Source : Note 492504 - Dependent customer exit-type variables
    I hope this might helps you a bit,
    Regards
    CSM Reddy

  • Determining angle between current and last position; 2D

    Hi,
    I have a player on a JPanel from a bird's eye view. I have to determine the angle (0 - 360) of the player's move:
                          0 or 360
                             |
                             |
                             |
                             |
    270--------------------Player--------------------90
                             |
                             |
                             |
                             |
                            180The top of the JPanel corresponds to 0 or 360 degree, the right side to 90 degree etc.
    I have to determine the angle between the current and the last (x, y) coordinates of the player. The player is being moved by mouse clicks.
    I don't know where to start to implement this... and how I can calculate the angle between these 2 positions (which I store in an Ellipse2D ArrayList).
    Thanks for your help!

    Multiply them by -1. Do you understand how degrees
    work? Degrees go counterclockwise. So 0 degrees
    should be on the right, and 90 degrees should be
    straight up.Oh... I didn't know that degrees in Java go counterclockwise and start on the right. Sorry. Is there a way to tell Java to start "at the top" and go clockwise? Or is the solution to change the formula? Thanks! :)

  • Report on Current and Last Year Sales

    Hi ,
    I have a requirement to create a report which would display in one column sales from current fiscal year/period and 2nd column sales from last fiscal year/period at the same time
    Please guide.

    Hi,
    You can achieve this by creating two selections in columns in query designer.
    First one restrict the key figure with current Fiscal period.
    Second restrict the key figure with Fiscal year with customer exit and -1 as offset value.
    Hope this helps........
    Rgs,
    Ravikanth.

  • How to create report which includes records of current month and last 7 days of previous month.

    Hi Experts,
    I need to create a report which includes records of current month and last 7 days of previous month.
    I will get records of current month by this formula :- month({PROBSUMMARYM1.OPEN_TIME})=month(currentdate)
    Please tell me how to add the records of last 7 days of previous Month for the same report.
    Thanks in Advance.

    Hi Ajay,
    If you have more than a year data in your database then your formula will return wrong results. ie. If your data consist of 2012,2013,2014 data then below formula will return all 8th month data irrespective of year. So, you need to check year also here
    month({PROBSUMMARYM1.OPEN_TIME})=month(currentdate)  and
    Year({PROBSUMMARYM1.OPEN_TIME})=Year(currentdate)
    Now add Abhilash second statement in OR so, your formula should look like :
    (month({PROBSUMMARYM1.OPEN_TIME})=month(currentdate)  and
    Year({PROBSUMMARYM1.OPEN_TIME})=Year(currentdate))
    OR
    Date({PROBSUMMARYM1.OPEN_TIME}) IN [DateAdd('d',-7,Maximum(LastFullMonth)), Maximum(LastFullMonth)
    -Sastry

  • How to show current year and last year sales in a WEBI Report

    Hi Guys
    How can show current YEar Sales in one column and Last YEar Sales in the other column based on a user prompt for the Current YEar Column.
    For Example is user enter 2010 for Year how can i show a Column for Sales-2010 and Sales 2009.
    Thanks

    If you can modify your Universe add an object named New Object Last Year whose SQL is:( yourTableName.Year + 1)
    Then in WebI create two distinct queries in your query Pane. In the first one you could do this:
    Query 1:
    objects: Year, Sales ... etc.
    filters:   Year Equal to '1. Prompt Year'
    Query 2:
    objects: Year, Sales ... etc.
    filters:    New Object Last Year Equal to '1.Prompt Year'
    Then in your report you can drag each object on their respective columns.
    If you don't want to use two distinct queries, use one like this:
    Query 1:
    objects: Year, Sales ... etc.
    filters:        Year Equal to '1. Prompt Year'
                 Or
                      New Object Last Year Equal to '1.Prompt Year'
    Edited by: PadawanGirl on Jun 23, 2011 6:28 PM

  • Finding Last friday and Last monday of current date

    Hi
    Please help me to get the Last friday and Last monday of current date using an sql query.
    If I am running query on 7th July 2008 ,it should give me Last friday as 07/04/2008 and Last monday as 07/07/2008
    I need to compare it with another date filed
    eg: Select * from employee where ReportedDate between [Last Friday] and [Last Monday]

    It works many times but fails for this scenario- If today is Friday and I run following query today, it throws today's date, which is incorrect. I need previous Friday's date to be returned everytime.
    select next_day (sysdate-7,'FRIDAY') Last_Friday from dual
    Edited by: user6402026 on May 22, 2013 1:43 PM
    Edited by: user6402026 on May 22, 2013 1:43 PM

  • First date and last date of current year

    Hi
    how can i get first and last date of current year, thanks for your help

    hi,
    use FM......... FIRST_AND_LAST_DAY_IN_YEAR_GET
    DATA: first LIKE sy-datum,
          last LIKE sy-datum.
    CALL FUNCTION 'FIRST_AND_LAST_DAY_IN_YEAR_GET'
      EXPORTING
        i_gjahr     = '2007'
        i_periv     = '24'
      IMPORTING
        e_first_day = first
        e_last_day  = last.
    WRITE: / 'First Date', first, '    Last Date', last.
    Regards
    CNU

  • Displaying current user's first and last name on a report.

    I would like to dispay the current user's name on a report. I've tried using User() and NQ_SESSION current user .
    These two options display the User ID (i.e. User name is Jon Smith. there ID is COMP/JSMITH.
    Using the two methods above the ID (COMP/JSMITH) is displayed. Is there a way I could display Jon Smith?
    I thought their might be a way to use Full Name or First and Last Name.
    Message was edited by:
    user618827

    VALUEOF(NQ_SESSION.DISPLAYNAME) will do the trick
    Mike L

  • In which table current /and in which table last 5 passwords are stored

    Hello
    in which table current /and in which table last 5 passwords are stored

    hi,
    The password may not be changed to any of a useru2019s last x passwords, if the user changes the password himself or herself.
    Until SAP NetWeaver 6.40 (inclusive), the password history was fixed to the value 5.
    After SAP NetWeaver 6.40, the administrator can set the size of the password history (up to 100 passwords selected by the user).
    The administrator can reset a useru2019s password to any initial password, therefore also to one of the last x passwords for this user. This is necessary, since the administrator should not know the passwords of the users. The user is prompted to change the initial password at the first interactive logon.
    You can change this with the profile parameter login/password_history_size.
    -Gokul

  • How to display records based on current month and last month???

    i have a query that want to display the data for current month and last month. when i try to view the reports, all the data/key figures is return all in 0.00.
    i modify the variables for this current month and last month but still the results is the same. i could not see the data. any advise on how to modify the variables for this current month and last month? i am thinking my variables is wrong.
    for the current month variable:
    the type of variable is characteristic value, processing by customer exit, reference characteristic is calendar year/month.
    thanks for all who are kind to help me here....

    Hi,
       If you install the variables from BC correctly and they are in active state, you can use them directly use them. check for this month the total values at info cube is ZERO or some value. as you told you are getting ZEROS for your input.and check the data at cube level based on your query design, apply all restrictions, fileters and see do you get any data at cube level and compare with report.

  • How to get current month and last month dynamically??

    how to get current month and last month dynamically
    like
    month = getCurrentMonth();
    lastmonth = getcurrentMonth() -1;
    please help
    thanks

    hi :-)
    /* depracated but can be still useful */
    java.util.Date dtCurrent = new java.util.Date();
    int month = dtCurrent.getMonth();
    int lastmonth = dtCurrent.getMonth() - 1;
    System.out.println("* " + month);
    System.out.println("* " + lastmonth);
    /* better to use this one */
    Calendar cal = new GregorianCalendar();     
    int imonth = cal.get(Calendar.MONTH);
    int ilastmonth = cal.get(Calendar.MONTH) - 1;
    System.out.println("*** " + imonth);
    System.out.println("*** " + ilastmonth);
    regards,

  • I am currently a full time college student and last term I had access to the creative cloud products, when I attempt to access InDesign I am asked to purchase products.  Please help.

    I am currently a full time college student and last term I had access to the creative cloud products, when I attempt to access InDesign I am asked to purchase products.  Please help.

    Do you have a subscription that you purchased and still pay into?  If so, contact Adobe support thru chat and see if they can assist you with your subscription.
    Chat support - For the link below click the Still Need Help? option in the blue area at the bottom and choose the chat option...
    Creative Cloud support (all Creative Cloud customer service issues)
    http://helpx.adobe.com/x-productkb/global/service-ccm.html ( http://adobe.ly/19llvMN )
    Did the school provide your access for free?  If so, check with the school to see if they still provide free access to the software for students.
    If neither of the above situations match yours, please provide more detail regarding what gave you access to the software.

  • Search employment verification database by first and last name

    hi
    can anyone please tell me from which info types do i have to get the data???
    this is in compensation management
    thanks
    sateesh

    Sateesh,
    I am assuming that you are trying to build a search help using SSN (surprised), first name and last name.
    I believe for this you can just use infotyypes 2 or 3. i dont what field in those infotypes your company is using to store SSN.
    I dont think you need to use any other ifoctypes like 267 etc.
    Thanks,

  • Need the first day of the year and last day of the year.

    hi all,
    i need a function module which can get me the first day of current year and last day of the current year??
    please help.

    Similar date questions are being asked ALL THE TIME.  Please search.

Maybe you are looking for

  • Is there a way to open Excell file from the server and display in the UI and save it back on to the

    Hello there, Is there a way to open Excell file from the server and display in the UI and save it back on to the server? (like showing xell file as a datagrid - add rows, columns etc.)

  • Please tell what to do if I see this error while opening an plan or while saving this plan?

    Error messsge: the enterprise global already contains a table named "Enterprise Entry". Do you want to replace the table with the one from the enterprise global, replace all items with duplicates, rename the table in the project, or cancel opening th

  • US iPad in the UK; return/exchange?

    My mum just placed an order in the US Apple Online Store for an iPad 2 as a birthday gift for me; she plans to send it to me when it arrives. I've just read, and was disheartened by, reports that many are experiencing severe backlight bleeding with t

  • Datasources activation

    Hi, I am trying to activate the datasources in RSA5 in ERP, i am getting the error 'DataSource 0MATERIAL_ATTR; switch to package ROMD is off Message no. R8371' Can anyone help to solve it. Thanks in advance, Ravi

  • Adding numbers with a length of 50 digits.

    Hi folks, Could any one please tell me how to add two numbers with 50 digits each. What should be the datatype for each column, in a table, to hold the above said precision. However, I know that NUMBER couldn't hold this b'coz its precision is upto 3