Get previous id

Hi folks,
Please help me on below. This is an example to my requirement. I need to get extra column 'PREV_ITEM_CD' which has the previous value of 'ITEM_CD' column
Thank you
with t as(
select 10 as id,1123 as item_cd,201301 as month_id from dual union all
select 10 as id,1124 as item_cd,201301 as month_id from dual union all
select 10 as id,1125 as item_cd,201301 as month_id from dual union all
select 10 as id,1126 as item_cd,201302 as month_id from dual union all
select 10 as id,1127 as item_cd,201302 as month_id from dual union all
select 10 as id,1128 as item_cd,201303 as month_id from dual union all
select 10 as id,1128 as item_cd,201303 as month_id from dual union all
select 10 as id,1129 as item_cd,201304 as month_id from dual union all
select 10 as id,1130 as item_cd,201304 as month_id from dual
select * from t
10 1123 201301
10 1124 201301
10 1125 201301
10 1126 201302
10 1126 201302
10 1128 201303
10 1131 201303
10 1129 201304
10 1129 201304
i need to get output -one more column as item_cd_prev as below (please assume 1122 will be previous value)
10 1123 1122  201301
10 1124 1122  201301
10 1125 1122  201301
10 1126 1125  201302
10 1126 1125  201302
10 1128 1126  201303
10 1131 1128  201303
10 1129 1131  201304
10 1129 1131  201304

How do you get the results you want?  Is month_id always an integer?  What role (if any) does the id column play in this problem?  Are the results you posted really what you want from the given sample data?  (The value 1131 does not appear in the WITH clause, but it does appear in the output.)
Depending on your answers:
WITH got_month_high_item_cd AS
    SELECT  id, item_cd, month_id
    ,       MAX (item_cd) OVER ( PARTITION BY  month_id
                                  ,            id  -- Maybe
                               )   AS month_high_item_cd
    FROM    t
--  WHERE ...      -- If you need any filtering, put it here
SELECT    id, item_cd
,         NVL ( LAST_VALUE (month_high_item_cd)
                     OVER ( PARTITION BY  id     -- Maybe
                            ORDER BY      month_id
                            RANGE BETWEEN UNBOUNDED PRECEDING
                                  AND     1         PRECEDING
              , 1122
              )  AS item_cd_prev_month
,         month_id
FROM      got_month_high_item_cd
ORDER BY  id -- Maybe
,         month_id
,         item_cd
Output from the sample data you posted (without 1131):
        10       1123               1122     201301
        10       1124               1122     201301
        10       1125               1122     201301
        10       1126               1125     201302
        10       1127               1125     201302
        10       1128               1127     201303
        10       1128               1127     201303
        10       1129               1128     201304
        10       1130               1128     201304

Similar Messages

  • How to get previous day of the date

    for a given date I need one day minus as a result date
    my input would be any date
    for example :
    Input Date : 2006-03-22 ( yyyy-mm-dd)
    Output should be : 2006-03-21
    If my input is 1st of April 2006, output i would expect is 31st March 2006
    Please suggest how to achieve this
    Thanks in advance

    Find below the code to get previous day of the current date
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    public class PreviosDay
    public static void main(String[] args)
    DateFormat dateFormat = new SimpleDateFormat( "MM/dd/yyyy");
    Calendar cl = Calendar.getInstance();
    // you can use a Date object's getTime() method , which is initialiazed
    // to desired date here directly ex: Date dt = new Date("10/10/2004");
    // cl.setTimeInMillis(dt.getTime());
    cl .setTimeInMillis(System.currentTimeMillis());
    cl.add(Calendar.DAY_OF_MONTH, -1);
    System.out.println("Previous date : "+dateFormat.format(
    new Date(cl.getTimeInMillis() ) ) );

  • I backed up my broken ipad, returned it to Apple got it replaced and have now accidently backup'd on my computer over the top of my original Ipad Back up.  how do I get previous copy of my back up??

    i backed up my broken ipad to my computer yesterday, returned it to Apple & got it replaced today and have now accidently backup'd on my computer over the top of my original Ipad Back up from yesterday.  Can anyone advise how do I get previous copy of my back up??

    Look at this knowledge base article on iOS backups to iTunes, note where the backups are kept for your type of computer, Windows or Mac, go to that Library location and see if there are any other files with the same name as the current backup but with a different extension and date/time stamp.  If so you can edit the extension of the current backup and then change the extension of the previous backup so it looks like what was on the current and the do a restore.  iTunes will think the older backup is now new and use it to restore.
    If there are no such files then you over-wrote the backup and it is gone.

  • How do you get previous Itunes pruchases onto a new Ipod without syncing it to the laptop?

    I've been in Australia for over a year, my previous Ipod broke so a purchased a new Ipod touch this year. However, as my Itunes library is on my laptop back in the UK, I'm a little depleted of music on this Ipod. Is it possible to get previous Itune purchases onto my Ipod here in Australia without me flying home, or worse still having the laptop sent over? I've kept all my Itunes receipts.

    Try here:
    Downloading past purchases from the App Store, iBookstore, and iTunes Store
    you might not be able to get some items back since yuo are in a differenrt country now.

  • How to get previous day data if i dont have current day data.

    Hello Gurus,
    I have a stock levels data in ODS. when there is no movements, we are not getting any stocks into ODS. So we have to get previous day data as it is for current day data into another ODS.
    Could you please help me in this regard.
    Thanks in advance,

    Rama -    
            0CALDAY can't help us in this scenario .
    Step 1 :
        To do this - You have to add one ZDATE (InfoObject ) to 1st ODS. ZDATE is updated by itself from current date of system  when ever you are loading data to 1st ODS.
    Step 2:
       You have to do full update to 2nd ods.At the selection screen of InfoPackage  (from 1st ODS to 2nd ODS ) you have to write following code for ZDATE.
    pseudo Code:
    1) Select fields "Rec_INSERT","Time stamp","Request Status" and "Request ID"  where ICUBE = ODS1 from table "RSMONICDP"
    2) Populate above selected fields data in INTERNAL TABLE
    3) Sort INTERNAL TABLE by Time stamp .
         If (Record Count = ' 0 ' for current date in internal table )
         update records from  ODS1 to ODS2 where ZDATE = "yesterday date"
         update records from ODS1 to ODS2 where ZDATE= "today date"
    Make sure this is full update not delta update from ODS1 to ODS2
    I am sorry, I m not good in Coding but I am sure if  u use this logic,You can meet your requirement.
    I hope you can understand my logic. Let me know if you have any questions,
    Anesh B .

  • Getting previous month

    could you please tell me how to get previous month?
    using sysdate gives me current date. from that i can get current month using to_char function. But how can I get the previous month?

    All you ned to remember is that there are two date units, days and months. Everything else is a multiple/fraction of these.
    To increment
    n days:    SYSDATE + n
    n hours:   SYSDATE + n/24
    n minutes: SYSDATE + n/24/60
    n seconds: SYSDATE + n/24/60/60
    n weeks:   SYSDATE + n*7
    n months:  ADD_MONTHS(SYSDATE,n)
    n years:   ADD_MONTHS(SYSDATE,12*n)

  • I just purchased two new itouches, how do I get previous purchase items on them, I just purchased two new itouches, how do I get previous purchase items on them

    I just purchased two new itouches, how do I get previous purchase items on them, I just purchased two new itouches, how do I get previous purchase items on them

    thank you. I didn't write what I meant correctly.  I own a first gen itouch whick I've had for quite a while and over the years I've purchased all kinds of things from music to short do I get all of those purchases on to my two new current gen itouches?  I was able to find and see all of my purchases since 2008 but don't know how to get it and download all of it. Can you help me?

  • Date Function to get Previous week

    Is there a date function to get
    Let us say today is 23rd of Jun 2009
    I want a date function to get previous Thursday 18 Jun 2009
    and another function which get 7 days before which is Friday 12 Jun 2009.
    FYI: For the current week, I was able to use:
    select next_day(case to_char(sysdate,'Day') when 'Thursday' then trunc(sysdate-1) else trunc(sysdate) end, 'Thursday')
    from dual;
    select next_day(trunc(sysdate-7), 'Friday') from dual;
    I want something similar to that.

    No, not separate functions, but you can tweak next_day to get what you want:
    select trunc(sysdate) today,
           next_day(trunc(sysdate)-1, 'THURSDAY')-7 last_thursday,
           next_day(trunc(sysdate)-1, 'FRIDAY')-14 prev_2nd_friday
    from   dual;
    23-JUN-09 18-JUN-09 12-JUN-09

  • Could not get Previous Week Day starting today

    I need help  to get PREVIOUS WEEK day starting today.
    Today is 15 / 09 / 2011
    I need Date 08 / 09 / 2011
    Hence both 15th and 08 are Thursday. One is today's thursday and other is previous week thursday.

    Warranty on iPhone is not international, but valid only in country of
    original sale. Since you purchased the iPhone in Australia, your warranty
    is valid only in Australia. You must either personally return the iPhone
    to Australia for evaluation and possible replacement or sent it to someone
    in Australia to take into Apple for the evaluation. Apple will not accept
    international shipments for repair nor will Apple ship out of the country
    after replacement.
    Sorry to be the bearer of this news, but it is the way iPhone warranty and
    replacement has always worked.

  • BAPI to get Previous Employment info

    does anybody know a BAPI call to get previous Employment informations of an employee?
    Employer, City, Job type, work contract?

    check the below link i think u can find the bapi required for u r application
    Try Bapi BAPI_EMPLOYEE_GETDATA. The table in which this data is stored are named PA****. Replace the **** with number of the infotype. Thus infotype 0001, organizational assignment, can be found in table PA0001.
    Good luck!

  • Get previous bill date using SQL

    I am table which holds records for bill generation. I have column name gene_bill_date which is date field and it holds a value the date on which the particular bill was generated.
    Now I am trying to get previous bill date, not the current bill date. I can to Max(gene_bill_date) to get current bill date, but how do I get previous bill date?

    Sorry, it's unclear what you're asking.
    You didn't post any sample data, so I'll use the scott.emp table to illustrate. Let's say we're interested in deptno=30 only, just to reduce the output from 14 rows to 6.
    If, for every row, you need to know the most recent gene_bill_date before the one on that row, you can do something like this:
    SELECT     ename
    ,     hiredate
    ,     LAG (hiredate) OVER (ORDER BY hiredate)     AS prev_hiredate
    FROM     scott.emp
    WHERE     deptno     = 30
    ALLEN      20-Feb-1981
    WARD       22-Feb-1981 20-Feb-1981
    BLAKE      01-May-1981 22-Feb-1981
    TURNER     08-Sep-1981 01-May-1981
    MARTIN     28-Sep-1981 08-Sep-1981
    JAMES      03-Dec-1981 28-Sep-1981Are you only interested in the 2 most recent dates in the whole result set?
    If so, do a Top-N Query , like this:
    WITH     got_r_num     AS
         SELECT     ename
         ,     hiredate
         ,     RANK () OVER (ORDER BY hiredate  DESC)     AS r_num
         FROM     scott.emp
         WHERE     deptno     = 30
    SELECT     *
    FROM     got_r_num
    WHERE     r_num     <= 2
    ENAME      HIREDATE         R_NUM
    JAMES      03-Dec-1981          1
    MARTIN     28-Sep-1981          2 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.

  • TSQL Get Previous values for each group by

    Dear Friends,
    I have a problem with my TSQL statment. 
    I need to get previous value for each day and specific unit. Everything goes fine if I have always 2 unit in each day. But if same day has just one unit or less, I cannot have the previous value in the current record.
    The Output is this one:
    20131112 2
    30.00 NULL
    20131112 3
    34.00 NULL
    20131113 2
    40.00 30.00
    20131113 3
    45.00 34.00
    20131114 2
    50.00 40.00
    I dont have the second record for 2013-11-14, because in this date I just have value for unit 2.
    The final output should include the record:
    20131114 3
    0 45.00
    The Statment I have is:
    [SK_DAY] [int] NOT NULL,
    [SK_UNIT] [int] NOT NULL,
    [VALUE] [decimal](18, 2) NULL,
    [SK_DAY] ASC,
    ) ON [PRIMARY]
    Thank you!!!

    Thank you Kalman,
    But my problem is not with date! Fortunately I have dates for all days.
    The problem is for dimension tables like UNITS that I dont have values for all days, and when I try to get the previous value for previous day and each unit I dont have records. 
    Could you help me??
    Thank you!!

  • I have a first gen ipod touch, is there a way to get previous versions of apps like spotify?

    I have heard that you can get previous versions of apps somewhere.  Does anyone know how to do this?  I believe it is running either ios 3 or 4...

    Not for a 1G. It works for 2G (IOs 4.2.1) and later only
    'Last Compatible' Version of Apps for Users Running Legacy Versions of iOS - Mac Rumors

  • How to get previous days execution time?

    Hello All,
    I have below 3 tables in my Oracle Database --
    1) Table jobs
    Job ID  Job Name
    1      ABC
    2      PQR
    3      XYZ
    2) Table Job_Tasks
    Task_ID   TASK_NAME  Job_ID 
      T1              T_ABC            1
      T2              T_ABE            1
      T3             T_ABD            1
      T4             T_PQS            2
      T5             T_PQT            2
      T6             T_PQU            2
      T7             T_XYA            3
      T8             T_XYZZ           3
      T9             T_XYYA           3
    3) Job_LOG_Table
    1111              1        ABC    06/09/2013 11:00     06/09/2013 13:00   COMPL
    1234              1        ABC    07/09/2013 11:30     07/09/2013 13:30   COMPL
    1235              1        ABC    08/09/2013 11:30     08/09/2013 13:30   COMPL
    1244              1        ABC    09/09/2013 11:00     09/09/2013 13:00   COMPL
    1354              1        ABC    10/09/2013 11:30      10/09/2013 13:30   RUNNING
    Question Is --
       I would like to display the job status from yesterday and from time 20:00 onwards.. ie. it should display the job_SEQ 1354. ( start_time >= (sysdate-1)||'20:00:00') and  also want to disaply previous days execution time for same job (ie. for 9th Sep).
      As I am displaying the jobs from yesterday 20:00 hour onwards.. I am unable to get previous execution time for the same job.
    I tried with Analylitical function LAG  but it's showing PREV_START_TIME.and PREV_END_TIME as BLANK.
    Could you please help me to resolve the issue?
    Thank you in advance.

    As suggessted by Mike,  below is the modification I have done in my sql and I think it's working now....
    I will check for more volume of data and confirm.  --
    select original_sql as
    select w.job_id,
      jobs w,
      job_log_table r
    select job_id,job_name,
    (select start_time from job_log_table j where j.job_id=os.job_id and j.start_time>=sysdate-2 and rownum <2) prev_start_time,
    from Original_sql os
    os.start_time>=to_date(to_char(sysdate-1,'DD/MM/YYYY') || ' 20:00:00','Dd/MM/YYYYHH24:MI:Ss')
    as of now, I am getting the below result --
    job_id,  job_name  prev_start_time,      prev_end_time,            curr_start_time,           curr_end_time,           status 
           1           ABC  09/09/2013 11:30   09/09/2013 13:00      10/09/2013 11:30          10/09/2013 13:30          RUNNING
    Thank you !

  • Client has only CS5.5 Indesign. I've CC. Can I get previous version to avoid .idml files?

    At the moment we're both using old CS4 Indesign to avoid the downsaving of files. Somebody said being with CC you can get previous versions too. Is this the case or is there another way around this please?

    No and no. Your client needs to upgrade if you do not want to use IDML. CS5.5 was never part of CC, so it's not available. CC started out with CS6.

  • HT201441 I don't have owner's phone number, how to get  previous owner's Apple ID(email address) and send him a email let him remove the device from the account.

    I don't have owner's phone number, how to get  previous owner's Apple ID(email address) and send him a email let him remove the device from the account

    There is no way for anyone on this forum to assist you.  Apple will also not assist you.  If you purchased the device thru ebay, file a claim.  If you purchased it some other way, I'm sorry.  there is nothing anyone can do to help you.

Maybe you are looking for

  • Plot showing problem with graph

    Hi everybody! Now I've come to dead end with my vi. I don't know why my vi doesn't show the trend line anymore. The previous version I did show the line, but now I've tried many things but the line wouldn't show up. The idea is that graph shows max,

  • Other growing at alarming rate

    when syncing my iphone 4s on the bottom of the page in itunes it shows (blue) for audio, (orange) for photos, (green) for apps, (yellow) for other. My question is what is OTHER? I have tried to find this out and can't seem to find anything on it. It

  • IBooks Author: superscript not displaying after export to iPad / iPhone

    superscript displays perfectly in iBooks Author on my Mac but reverts to normal baseline in iBooks on iOS. I have tried with different font - same problem. What could the problem be?

  • I got a new ipad mini from the pawn shop how do I get my warranty!!

    help me with my question please

  • Problems with moving directories

    Hi, i have a problem with moving one subdirectory from one directory to another. Here is the code      public static boolean moveToDir(String filename, String dirname)      if( XFileUtils.fileExists(filename) )      return onMoveSingleFileToDir(filen