How to get the last different occurrence in a SCD table

hey,
i have a slowly changing dimension and i want to get the last different occurrence for each row.
Sample Data
the field LAST_VALUE is the one i'm looking for.
ID     START_DATE     END_DATE     VALUE     LAST_VALUE
1     01/01/2000     01/02/2000     X     null
1     01/02/2000     01/03/2000     X     null
1     01/03/2000     01/04/2000     Y     X
1     01/04/2000     01/05/2000     Y     X
1     01/05/2000     01/06/2000     X     Y
1     01/06/2000     01/07/2000     X     Y
1     01/07/2000     01/08/2000     Z     X
1     01/08/2000     01/09/2000     Q     Z i want to try and do that without three sub selects.
i've tried analytic functions but lag brings me the last occurrence which is not necessarily different.
thanks.

Something like this :
SQL> with sample_data as (
  2    select 1 ID, to_date('01/01/2000','DD/MM/YYYY') START_DATE, to_date('01/02/2000','DD/MM/YYYY') END_DATE, 'X' VALUE from dual union all
  3    select 1, to_date('01/02/2000','DD/MM/YYYY'), to_date('01/03/2000','DD/MM/YYYY'), 'X' from dual union all
  4    select 1, to_date('01/03/2000','DD/MM/YYYY'), to_date('01/04/2000','DD/MM/YYYY'), 'Y' from dual union all
  5    select 1, to_date('01/04/2000','DD/MM/YYYY'), to_date('01/05/2000','DD/MM/YYYY'), 'Y' from dual union all
  6    select 1, to_date('01/05/2000','DD/MM/YYYY'), to_date('01/06/2000','DD/MM/YYYY'), 'X' from dual union all
  7    select 1, to_date('01/06/2000','DD/MM/YYYY'), to_date('01/07/2000','DD/MM/YYYY'), 'X' from dual union all
  8    select 1, to_date('01/07/2000','DD/MM/YYYY'), to_date('01/08/2000','DD/MM/YYYY'), 'Z' from dual union all
  9    select 1, to_date('01/08/2000','DD/MM/YYYY'), to_date('01/09/2000','DD/MM/YYYY'), 'Q' from dual
10  )
11  select v.id
12       , v.start_date
13       , v.end_date
14       , v.value
15       , last_value(case when value != prev_value then prev_value end ignore nulls)
16               over(partition by id order by start_date) as "LAST_VALUE"
17  from (
18    select t.*
19         , lag(value) over(partition by id order by start_date) as prev_value
20    from sample_data t
21  ) v
22  ;
        ID START_DATE  END_DATE    VALUE LAST_VALUE
         1 01/01/2000  01/02/2000  X    
         1 01/02/2000  01/03/2000  X    
         1 01/03/2000  01/04/2000  Y     X
         1 01/04/2000  01/05/2000  Y     X
         1 01/05/2000  01/06/2000  X     Y
         1 01/06/2000  01/07/2000  X     Y
         1 01/07/2000  01/08/2000  Z     X
         1 01/08/2000  01/09/2000  Q     Z
8 rows selected

Similar Messages

  • How to get the last time a record was modified or created ?

    Is there a way (hidden system fields for example, system view, other ?) to get the last time a record of a table was modified or the date it was created.
    Thanks for any help.
    Stiphane CAMPION

    blarman74 wrote:
    I know I could do this with a trigger, but I was wondering if there is a data dictionary view I can query to find the date/time a table was last updated. What I have is a table that is constantly getting hammered with new data being uploaded from a third party. We need to keep close tabs to verify that this datafeed is constantly running and bringing in new data. Because the data that comes in comes from multiple timezones and there are hundreds of lines, our attempts at monitoring by just looking at the data leave something to be desired.
    Any ideas?refer this links
    How to find Last modified/updated time of a particular table
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1590655700346557237
    Thanks

  • How to get the last day of the week?

    Hii
    i can get the calender week number for any given date using
    SELECT to_char(to_date('04/04/2011','MM/DD/YYYY'),'WW') FROM dual
    can any body tell me, how to get the last day of that week ?
    and the answer should be 04/08/2011(8th april )
    thanks
    San
    Edited by: sandeep9 on Apr 4, 2011 3:50 AM

    Hi, San,
    Here's one way:
    WITH     sample_data     AS
         SELECT  DATE '2011-04-04'     AS dt
         FROM     dual
    SELECT  dt
    ,     TO_CHAR (dt, 'WW')     AS week_num
    ,     NEXT_DAY ( dt - 1
               , TO_CHAR ( TRUNC (dt, 'YEAR') - 1
                      , 'Day'
               )          AS end_o_week
    FROM     sample_data;Another way is to use date arrithmetic:
    WITH     sample_data     AS
         SELECT  DATE '2011-04-09'     AS dt
         FROM     dual
    SELECT  dt
    ,     TO_CHAR (dt, 'WW')     AS week_num
    ,     TRUNC (dt, 'YEAR')
          + (7 * CEIL ( (dt - (TRUNC (dt, 'YEAR') - 1))
                / 7
          - 1               AS using_date_arithmetic
    FROM     sample_data;

  • How to get the last day of a month?

    HI,
    I want to know how to get the last day of a month.
    In my JClient form, I tried to get it by using oracle.sql.Date method, that is:
    lastday=oracle.sql.Date anydate.lastDayOfMonth();
    But it does not work. The result is lastday=anydate.
    Why?
    Stephen

    You can use the Calender class...
    Calendar c = Calendar.getInstance();
    and then something like...
    c.add(c.MONTH, 1);
    int dayOfMonth = c.get(Calender.MONTH);
    c.add(c.DAY_OF_MONTH, - (dayOfMonth-1) );
    other usefull functions are:
    System.out.println(" YEAR : " + c.get(Calendar.YEAR));
    System.out.println(" MONTH : " + c.get(Calendar.MONTH));
    System.out.println(" DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
    System.out.println(" DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
    System.out.println(" DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
    System.out.println(" WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
    System.out.println(" WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
    System.out.println(" DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println(" HOUR : " + c.get(Calendar.HOUR));
    System.out.println(" AM_PM : " + c.get(Calendar.AM_PM));
    System.out.println(" HOUR_OF_DAY (24-hour): " + c.get(Calendar.HOUR_OF_DAY));
    System.out.println(" MINUTE : " + c.get(Calendar.MINUTE));
    System.out.println(" SECOND : " + c.get(Calendar.SECOND));
    System.out.println();*/

  • How to get the last day according to fiscal period input in selection scree

    Hello expert
    how to get the last day of fiscal period input.
    the fiscal period inculdes 1-16
    when fiscal period is greater than 12, only calculate the last day of 12nd month
    your solution will be apprecaited, FM existing?
    thank you
    Kevin

    Hi,
    when you give a particular date in any month
    the following fm will give you the last date of that month
    here you can give
    R_FDATE-HIGH  as 01 and month as the period you wnat and year for current year
    concatenates '01'  month year  into r_fdate-high separated by '.'.
    then it will give g_ltdt for that month and year which wil be the last date of that month
        CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
          EXPORTING
            DAY_IN            = R_FDATE-HIGH
          IMPORTING
            LAST_DAY_OF_MONTH = G_LTDT
          EXCEPTIONS
            DAY_IN_NO_DATE    = 1
            OTHERS            = 2.
        IF SY-SUBRC <> 0.
          MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
    thanks & regards,
    Venkatesh

  • How to get the last error for while loop?

    How to get the last error for while loop? I just want transfer the last error for while loop, but the program use the shift register shift all error every cycle.

    What do you mean by "get" and "transfer"?
    If the shift register is not what you want, use a plan tunnel instead.
    Typically, programmers are interested in the first, not last, error.
    Can you show us your code so we have a better idea what you are trying to?
    LabVIEW Champion . Do more with less code and in less time .

  • How to get the last version of flash in MSI format automatically?

    How to get the last version of flash in MSI format automatically?
    Roberto Neigenfind
    Bravo Tecnologia
    www.bravotecnologia.com.br

    Hi Barbara,
    Flash Professional CS5.5 is a 32-bit application which can be installed on computers with either 32-bit or 64-bit operating systems.
    You can purchase this by Adobe's backward Licensing policy :
    " Adobe allows program members to order a current-version license but use a prior version. These members can contact Adobe Customer Service to request a serial number for the earlier version if they do not already have one. Prior-version software is available via ESD and can be purchased through standard resellers. The program member must follow all guidelines of the current-version EULA. "
    Please check the doc : http://www.adobe.com/volume-licensing/policies.html

  • How to get the "last changed by" for a set of function modules?

    How to get the "last changed by" for a set of function modules?
    is there any table to get it??

    See [this|Re: Date of creation of function module] I posted earlier.
    >TFDIR will give you the name of the function group program and the include number.
    >E.g. SAPLZFUNCGROUP Include 01.
    >From this you can construct the include name: LZFUNCGROUPU01.
    >You can look this up in TRDIR to find the creation date (CDAT) of the function module.
    In your case, you need unam and udat.
    matt

  • How to get the last page  SAP Script form

    How to get the last page  SAP Script form.
    I want to print a specific information in the last page of SAP form (Script). Please tell me how to get the last page number.
    Regards

    Hi
    You have to check the system variable &NEXTPAGE&, if it's 0 it means you're in the last page.
    From SAP Help:
    This symbol is used to print the number of the following page. The output format is the same as with &PAGE& .
    Note that on the last page of the output, in each window that is not of type MAIN, &NEXTPAGE& has the value 0.
    /: IF &NEXTPAGE& = '0'
       Last page
    /: ENDIF
    Max

  • How to get the data from multiple nodes to one table

    Hi All,
    How to get the data from multiple nodes to one table.examples nodes are like  A B C D E relation also maintained
    Regards,
    Indra

    HI Indra,
    From Node A, get the values of the attributes as
    lo_NodeA->GET_STATIC_ATTRIBUTES(  IMPORTING STATIC_ATTRIBUTES = ls_attributesA  ).
    Similarily get all the node values from B, C, D and E.
    Finally append all your ls records to the table.
    Hope you are clear.
    BR,
    RAM.

  • How to get the data from pcl2 cluster for TCRT table.

    Hi frndz,
    How to get the data from pcl2 cluster for tcrt table for us payroll.
    Thanks in advance.
    Harisumanth.Ch

    PL take a look at the sample Program EXAMPLE_PNP_GET_PAYROLL in your system. There are numerous other ways to read payroll results.. Pl use the search forum option & you sure will get a lot of hits..
    ~Suresh

  • How to get the object class field value in CDHDR table for vendor

    hi
    how to get the object class field value in CDHDR table for vendor

    Try KRED/KRED_N as object class in CDHDR for Vendor.

  • How to get the last revision quote to SSRS report

    HI,
    My report current result like this
    Qoute ID
    Revision
    Q1
    1
    Q1
    2
    Q1
    3
    Q2
    1
    Q3
    1
    I would like to filter if the quote ID is same , only get the last revision quote into report
    Qoute ID
    Revision
    Q1
    3
    Q2
    1
    Q3
    1
    How can i compare the Quote ID and Revision in fetchxml or dataset filter ?
    Thank You

    <fetch distinct='false' mapping='logical' aggregate='true'> 
        <entity name='Quote'> 
           <attribute name='revisionnumber' alias='estimatedvalue_max' aggregate='max' /> 
           <attribute name='quotenumber' alias='quoteid' groupby='true' /> 
        </entity> 
    </fetch>"
    Regards, Saad

  • How to get the last seven days for out put of present day

    Hi Experts,
    we developed one query for 3 years data
    1.user enter the current date (21-03-2014) but out put value should be last seven days (21-03-2014 - 7) (last one week data only)
    2.some times user will enter the (20-03-2014 ) but out put value should be last seven days (20-03-2014 - 7) (last one week data only)
    how to achieve the 2 different scenario?
    is there any standard variable ? please suggest .
    Thanks,
    Bindu.

    Hi,
    i created the Characteristic value variable not formula variable
    in char variable i can't see the off set functionality but we can see off set function in formula variable
    user requirement he wants enter the input value as date and he need last week data means one week data calday wise.
    i tired the 0PREVWK , 0CWEEK these two variables these not asking the input and direct showing the out put.
    these can achieve through the customer exit?
    Thanks,
    Bindu.

  • How to get the Last cell in HSSFCell (Excel sheet)

    Hello
    I am trying to convert an excel sheet to a tab limited file.
    I am using HSSF and unable to track how do i know if the cell encountered is a last filled cell in excel sheet
    Some of the cells in the sheet can be blank. In that case i am just using inputting a '\t' for that cell and read the next one. But coz of this when i get the last cell, a tab is included for that too.
    Can someone let me know how can i rectify this?
    it seems like HSSFCell does not have any methos like lastCell or so
    Thanx

    then use getLastCellNum() in org.apache.poi.hssf.usermodel.HSSFRow

Maybe you are looking for

  • Songs won't continuous play in playlist on itunes, Please Help!!

    Just recently when I try to play my music on my computer on itunes it won't continuous play in my playlists. Just one song at a time. I cannot figure it out. Can anyone help? Any suggestions?

  • Time machine over a network volume

    So since Snow Leopard (and I think Leopard), when you use Time machine to back up over a network share (for example, a USB drive attached to a mini server), the OSX creates a sparsebundle image where all time machine files are stored. In Lion, If I e

  • Messages alert song in "stand by" mode??

    When my IPhone4s going to a "stand by" whit a login to recover the Messages are NOT produzindo a alert song more! That's terrible!! The Messages are with a alert song working well and when the phone go to stand by they stop to producente a alert song

  • How do I fix my ipod with a blank screen?

    How do I fix my ipod with a blank screen?

  • Extend material to knew warehouse

    Hi,         We need to extend several materials to a knew warehouse. Wich is the best practice? Regards Enzo