Consecutive date query... Please help

Dear All,
I have this table "att_emp_leaves" that records employees leaves information in the following columns:
Employee_ID number(8)
From_Date date
To_Date
Now I have to trace records that have consectutive date records, like leave taken again when the previous leaves ends up. The following records will help you understand this.
Employee_ID , From_Date , To_Date
2711, 19-DEC-07, 07-JAN-08
2711, 08-JAN-08 , 10-JAN-08
or
3134 , 12-DEC-07 , 03-JAN-08
3134 , 04-JAN-08 , 04-JAN-08
In first case employee applied for leaves from 19-dec to 7-jan and then the very next date again from 8-jan till 10-jan.
In second record set again employee having id 3134 applies leaves just after his previous leaves were ended up. To date of the first record is 03-JAN while 04-Jan just comes after 3-jan i.e. 4-jan.
There are other records too in this table that might not have consetuive entries of dates and they are to be ignored, records like
14210 , 01-JAN-08, 02-JAN-08
14210 , 09-JAN-08, 10-JAN-08
This record is to be ignored.
Kindly help me write such a query. I will be very greatful to you.
Regards, Imran Baig

Here's a version that will return 1 record per employee_id per sequence of multiple leaves. I added some additional test data to to show that it will handle leave sequences greate than 2, as well as multiple leave sequences. Employee_id 2711 has single sequence of 3 consecutive leaves while employee_id 3134 has two sequences one of length 2 and another 1 year later of length 3.
with att_emp_leaves as (select 2711 employee_id, to_date('19-DEC-07','dd-mon-yy') from_date, to_date('07-JAN-08','dd-mon-yy') to_date from dual
  union all select 2711, to_date('08-JAN-08','dd-mon-yy'), to_date('10-JAN-08','dd-mon-yy') from dual
  union all select 2711, to_date('11-JAN-08','dd-mon-yy'), to_date('13-JAN-08','dd-mon-yy') from dual
  union all select 3134, to_date('12-DEC-07','dd-mon-yy'), to_date('03-JAN-08','dd-mon-yy') from dual
  union all select 3134, to_date('04-JAN-08','dd-mon-yy'), to_date('04-JAN-08','dd-mon-yy') from dual
  union all select 3134, to_date('12-DEC-08','dd-mon-yy'), to_date('03-JAN-09','dd-mon-yy') from dual
  union all select 3134, to_date('04-JAN-09','dd-mon-yy'), to_date('04-JAN-09','dd-mon-yy') from dual
  union all select 3134, to_date('05-JAN-09','dd-mon-yy'), to_date('07-JAN-09','dd-mon-yy') from dual
  union all select 14210,to_date('01-JAN-08','dd-mon-yy'), to_date('02-JAN-08','dd-mon-yy') from dual
  union all select 14210,to_date('09-JAN-08','dd-mon-yy'), to_date('10-JAN-08','dd-mon-yy') from dual
), t1 as (
  select d.*,
      connect_by_root from_date original_from_date,
      sys_connect_by_path(d.to_date,'->') end_dates,
      level leave_cnt
  from att_emp_leaves d
  connect by prior employee_id = employee_id and prior d.to_date = from_date-1
  start with (employee_id, from_date) in
    (select employee_id, from_date
     from att_emp_leaves d1
     where not exists
       (select 1 from att_emp_leaves d2
        where d2.employee_id=d1.employee_id and d2.to_date = d1.from_date-1))
), t2 as (
  select t1.*, rank() over (partition by employee_id, original_from_date order by leave_cnt desc) rn
  from t1
select t2.*, t2.to_date-original_from_date leave_duration from t2
where rn=1 and leave_cnt > 1
order by employee_id, original_from_date, to_date
EMPLOYEE_ID FROM_DATE   TO_DATE     ORIGINAL_FROM_DATE  END_DATES                                LEAVE_CNT  RN  LEAVE_DURATION
2711        11-JAN-2008 13-JAN-2008 19-DEC-2007         ->07-JAN-2008->10-JAN-2008->13-JAN-2008  3          1   25            
3134        04-JAN-2008 04-JAN-2008 12-DEC-2007         ->03-JAN-2008->04-JAN-2008               2          1   23            
3134        05-JAN-2009 07-JAN-2009 12-DEC-2008         ->03-JAN-2009->04-JAN-2009->07-JAN-2009  3          1   26            
3 rows selected

Similar Messages

  • Java Date query -- Please help...Really Stuck!!!

    My application gets a date from the database. I also create a new Date() to hold todays date. I need to be able to calculate the number of hours between the 2 if the days are the same or the no of days/hours if the days/month/year are different
    Can anyone help me with some code that might help me do this
    Thanks in advance

    import java.util.Calendar;
    import java.util.Date;
    public final class DateUtils {
        private static void displayDifference( final Calendar c1, final Calendar c2, final int field, final String displayText ) {
            System.out.println( displayText + " " + Math.abs( c1.get( field ) - c2.get( field ) );
        public static final void dateDiff( final Date d1, final Date d2 ) {
            final Calendar c1 = Calendar.getInstance();
            final Calendar c2 = Calendar.getInstance();
            c1.setTime( d1 );
            c2.setTime( d2 );
            displayDifference( c1, c2, "Years: ", Calendar.YEAR );
            displayDifference( c1, c2, "Days: ", Calendar.DAY_OF_YEAR );
            displayDifference( c1, c2, "Hours: ", Calendar.HOUR );
            displayDifference( c1, c2, "Minutes: ", Calendar.MINUTE );
            displayDifference( c1, c2, "Seconds: ", Calendar.SECOND );
    }

  • I recently have ipad but before i sell it i made a back up of it and by an iphone when i put all the date on my iphone some files and software did not install is there anything i can do to recover the lost data? please help

    i recently have ipad but before i sell it i made a back up of it and by an iphone when i put all the date on my iphone some files and software did not install is there anything i can do to recover the lost data? please help

    I don't think you're on iOS 5, I think you're using iOS 6.  That's the latest version.
    Unless you've used iCloud to back up your documents, you won't be able to restore them.  And for future reference, you don't have to uninstall Pages to update your iPad anymore.  Sorry about this.

  • How to improve the performance of the attached query, Please help

    Hi,
    How to improve performance of the below query, Please help. also attached explain plan -
    SELECT Camp.Id,
    rCam.AccountKey,
    Camp.Id,
    CamBilling.Cpm,
    CamBilling.Cpc,
    CamBilling.FlatRate,
    Camp.CampaignKey,
    Camp.AccountKey,
    CamBilling.billoncontractedamount,
    (SUM(rCam.Impressions) * 0.001 + SUM(rCam.Clickthrus)) AS GR,
    rCam.AccountKey as AccountKey
    FROM Campaign Camp, rCamSit rCam, CamBilling, Site xSite
    WHERE Camp.AccountKey = rCam.AccountKey
    AND Camp.AvCampaignKey = rCam.AvCampaignKey
    AND Camp.AccountKey = CamBilling.AccountKey
    AND Camp.CampaignKey = CamBilling.CampaignKey
    AND rCam.AccountKey = xSite.AccountKey
    AND rCam.AvSiteKey = xSite.AvSiteKey
    AND rCam.RmWhen BETWEEN to_date('01-01-2009', 'DD-MM-YYYY') and
    to_date('01-01-2011', 'DD-MM-YYYY')
    GROUP By rCam.AccountKey,
    Camp.Id,
    CamBilling.Cpm,
    CamBilling.Cpc,
    CamBilling.FlatRate,
    Camp.CampaignKey,
    Camp.AccountKey,
    CamBilling.billoncontractedamount
    Explain Plan :-
    Description                    Object_owner          Object_name     Cost     Cardinality     Bytes     
    SELECT STATEMENT, GOAL = ALL_ROWS                              14     1     13
    SORT AGGREGATE                                                  1     13
    VIEW                         GEMINI_REPORTING               14     1     13
    HASH GROUP BY                                        14     1     103
    NESTED LOOPS                                        13     1     103
    HASH JOIN                                             12     1     85
    TABLE ACCESS BY INDEX ROWID     GEMINI_REPORTING     RCAMSIT          2     4     100
    NESTED LOOPS                                        9     5     325
    HASH JOIN                                        7     1     40
    SORT UNIQUE                                        2     1     18
    TABLE ACCESS BY INDEX ROWID     GEMINI_PRIMARY          SITE          2     1     18
    INDEX RANGE SCAN          GEMINI_PRIMARY          SITE_I0          1     1     
    TABLE ACCESS FULL          GEMINI_PRIMARY          SITE          3     27     594
    INDEX RANGE SCAN          GEMINI_REPORTING     RCAMSIT_I     1     1     5     
    TABLE ACCESS FULL     GEMINI_PRIMARY     CAMPAIGN                    3     127     2540
    TABLE ACCESS BY INDEX ROWID     GEMINI_PRIMARY          CAMBILLING     1     1     18
    INDEX UNIQUE SCAN     GEMINI_PRIMARY     CAMBILLING_U1                    0     1

    Hello,
    This has really nothing to do with the Oracle Forms product.
    Please, send the SQL or/and PL/SQL questions in the corresponding forums.
    Francois

  • Hello, okay? since then, was robbed today, and my find my    iphone this down, please tell me if he has a way to locate the computer, I need the iphone again, it has many important data it please help me

    hello, okay? since then, was robbed today, and my find my
      iphone this down, please tell me if he has a way to locate the computer, I need the iphone again, it has many important data it please help me

    It's gone and you most likely are not getting it back.
    If you had important info on it, you should have made backups so you would not lose the data.

  • I ordered an iphone 6 plus 64 gig space gray on 9/19/14. I still have not gotten any information on the ship date. Please help verizon!!!

    I ordered an iphone 6 plus 64 gig space gray on 9/19/14. I still have not gotten any information on the ship date. Please help verizon!!!

        urdea123,
    You called and help has arrived. Congrats on the new phone. The iPhone 6 Plus 64GB space gray device is shipping on 11/7. You can always check your order here http://vz.to/1qKj0Rx
    RobinD_VZW
    Follow us on twitter @VZWSupport

  • Just installed OS Yosemite, and now I can't open Illustrator CS6. Asks me to update my Java, but I check and my Java is up to date. Please help!

    Just installed OS Yosemite, and now I can't open Illustrator CS6. Asks me to update my Java, but I check and my Java is up to date. Please help!

    Some people have reported reinstalling the Apple 2014-001 Java update has helped.
    See thread 7 here  Illustrator CC 2014 crashes on startup after Yosemite 10.10 upgrade today.

  • I don't have wifi connection.I need to update my software to ios 8.0.2.How can I download it using cellular data?Please help

    I don't have wifi connection.I need to update my software to ios 8.0.2 using cellular data.How can I download using cellular data?.Please help.

    There are often places to go which have free WiFi ie coffee shops, shopping malls, fast-food places.
    You can connect the phone to your computer & download/update via iTunes if you have one.

  • I am using Iphone 4S and Ipad 2...it seems that my App Store application is missing or deleted. How can i retrieve it? How do I download a new one. My software is up to date. Please help

    I am using Iphone 4S and Ipad 2...it seems that my App Store application is missing or deleted. How can i retrieve it? How do I download a new one. My software is up to date. Please help asap. Thank you.

    Check: Settings - General - Restrictions - Installing Apps = ???

  • Need help with query that can look data back please help.

    hi guys i have a table like such
    CREATE TABLE "FGL"
        "FGL_GRNT_CODE" VARCHAR2(60),
        "FGL_FUND_CODE" VARCHAR2(60),
        "FGL_ACCT_CODE" VARCHAR2(60),
        "FGL_ORGN_CODE" VARCHAR2(60),
        "FGL_PROG_CODE" VARCHAR2(60),
        "FGL_GRNT_YEAR" VARCHAR2(60),
        "FGL_PERIOD"    VARCHAR2(60),
        "FGL_BUDGET"    VARCHAR2(60)
      )and i have a data like such
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','1','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','2','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7470','4730','02','10','2','200');I bascially need to get the total of the budget column. however its not as simple as it sound(well atleast not for me.) the totals carry over to the new period. youll noticed the you have a period column. basically what im saying is that
    fgl_grant_year 10 period 1 = for account 7600 its $100 and $100 for period 2 you see 100 dollars again this is not to be added this is the carried over balance. which remains $100.
    so im trying to write a query that basically does the following.
    im given a period for the sake of this example lets say period 1 i get nothing else. I have to find the greates grant year grab the amount for period 14(which is the total from the previous year) and add it to the amount of the current period. in this case period 1 grnt_year 11
    so the expected outcome should be $700
    240055     240055     7240     4730     02     10     14     200
    240055     240055     7600     4730     02     10     14     100
    240055     240055     7600     4730     02     11     1     400keep in mind that im not given a year just a period.
    any help that you guys can offer would be immensely appreciated. I have been trying to get this to work for over 3 days now.
    finally broke down and put together this post
    Edited by: mlov83 on Sep 14, 2011 8:48 PM

    Frank
    wondering if you can help me modify this sql statement that you provided me with .
    table values have been modified a bit.
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','00','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7200','4730','02','10','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7600','4730','02','10','1','400');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','2','100');
    Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','11','2','600');i need to take one more thing into consideration. if the greatest year has a value on period 00 i need to ignore the period 14 and the current period total would be
    the current period +(current period - greatest year 00)
    hope that makes sense so in other words with the new data above. if i was querying period two of grant year 11. i would end up with $800
    because the greatest year is 11 it contains a period 0 with amount of $400 so my total should be
    period 2 amount $ 600
    period 0 amount $ 400 - period 2 amount of $600 = 200
    600+200 = $800
    if i query period 1 of grant 360055 i would just end up with 800 of grnt year 10.
    i have tried to modify that query you supplied to me with no luck. I have tried for several day but im embarrased to say i just can get it to do what im trying to do .
    can you please help me out.
    Miguel

  • Sql query for quarter data..Please help

    Dear Experts,
    Please help with this query...
    i have data like below:
    Year
    Quarter
    MRR
    MRR%
    2012
    Q1
    10
    Q2
    30
    Q3
    50
    Q4
    60
    2013
    Q1
    20
    Q2
    30
    Now i need to caluclate MRR % column values as below
    for 2012 Q2 MRR % = (Q2MRR-Q1MRR)/Q1*100 that is (30-10)/10*100
    for 2012 Q3 MRR % = (Q4MRR-Q3MRR)/Q2*100
    for 2012 Q4 MRR %=  (Q1MRR-Q4MRR)/Q1*100  here q1 of 2013 and q4 of 2012...
    like this it wll go on..
    Please help with me query compute values in MRR % column.
    Best regards
    asp

    Maybe NOT TESTED! No Database at hand
    select year,quarter,mrr,
           ratio_to_report(q_mrr) over (partition by year order by quarter) pct_mrr
      from (select year,quarter,mrr,
                   mrr - lag(mrr,1,0) over (partition by year order by quarter) q_mrr
              from t
    Regards
    Etbin

  • To get the transaction data..please help

    Hi all,
    i need to retrive the transaction datas..like deleted ,n updated datas..
    I tried this query
    SELECT * FROM FLASHBACK_TRANSACTION_QUERY WHERE TABLE_OWNER='CFMSDEV'
    AND TABLE_NAME='M_REGIONS'
    but this is givng the feilds of details..not showing a fied what data is translated..
    please help me...
    thnks&regards

    Hi,
    To my knowledge this can be achieved only by Change Data Capture in 10g.
    Let us wait for others opinion.
    Regards
    K.Rajkumar

  • How to make recursive query.Please help

    Dear Experts:
    I want to retrieve all employees located in a department in addition to all other employees located in the child's nodes of this department too.
    Problem Details:
    I have "Employees" table and "Departments" Table
    The structure of Dept Table is:
    ID   primary key
    parent_dept   foreign key(id)
    deptname  varchar2
    deptType varchar2
    The Employee table structure is
    ID primary key
    dept_id foreign key(Department.id)
    empName varchar2Sample data for departments
    ID : 1
    parent_dept : null
    deptname: General Manager office
    deptType : 'GM'
    ID :=2
    parent_dept : 1
    deptname: Information tech.
    deptType : 'DPT'
    ID :=3
    parent_dept : 2
    deptname: Software Development
    deptType : 'SECTION'Sample Data for employees
    ID : 101
    dept_id  :1
    empName  King
    ID : 102
    dept_id  :2
    empName  ALAN
    ID : 103
    dept_id  :2
    empName  SAM
    ID : 104
    dept_id  :3
    empName  JANEI want to create a query that accepts a parameter "p_department_id" and returns All employees on the following conditions
    1- In case the parameter value is null , then retrieve All Employees "king - alan- sam-jane"
    2- In Case the parameter value is 1 , then retrieve all the employees under department id =1 in addition to all the employees located under the children departments.
    in this case it will be "king - alan- sam-jane"
    3- In case parameter value is 2 , then return all the employees under department id =2 in addition to all the employees located under the children departments.
    In this case it will be " alan- sam-jane"
    4- In case parameter value is 3 , then return all the employees under department id =3 in addition to all the employees located under the children departments.
    in this case it will be only "JANE"
    In brief , If I pass any value to the parameter :p_department_id , I want to retrieve all employees located in this department in addition to other employees located in the child's nodes of this department id
    I use oracle database 11g release 2
    Please help me
    Thanks
    Edited by: ta**** on Apr 3, 2013 5:56 PM
    Edited by: ta**** on Apr 3, 2013 5:58 PM

    SQL> variable p_department_id number
    SQL> exec :p_department_id := null
    PL/SQL procedure successfully completed.
    SQL> with employees as (
      2                     select 101 id,1 dept_id,'King' empName from dual union all
      3                     select 102,2,'ALAN' from dual union all
      4                     select 103,2,'SAM' from dual union all
      5                     select 104,3,'JANE' from dual
      6                    ),
      7     departments as (
      8                     select 1 id,null parent_dept,'General Manager office' deptname,'GM' deptType from dual union all
      9                     select 2,1,'Information tech.','DPT' from dual union all
    10                     select 3,2,'Software Development','SECTION' from dual
    11                    )
    12  select  *
    13    from  employees
    14    where dept_id in (
    15                      select  id
    16                        from  departments
    17                        start with (
    18                                       (
    19                                            :p_department_id is null
    20                                        and
    21                                            parent_dept is null
    22                                       )
    23                                    or
    24                                       id = :p_department_id
    25                                   )
    26                        connect by parent_dept = prior id
    27                     )
    28  /
            ID    DEPT_ID EMPN
           101          1 King
           102          2 ALAN
           103          2 SAM
           104          3 JANE
    SQL> exec :p_department_id := 1
    PL/SQL procedure successfully completed.
    SQL> /
            ID    DEPT_ID EMPN
           101          1 King
           102          2 ALAN
           103          2 SAM
           104          3 JANE
    SQL> exec :p_department_id := 2
    PL/SQL procedure successfully completed.
    SQL> /
            ID    DEPT_ID EMPN
           102          2 ALAN
           103          2 SAM
           104          3 JANE
    SQL> exec :p_department_id := 3
    PL/SQL procedure successfully completed.
    SQL> /
            ID    DEPT_ID EMPN
           104          3 JANE
    SQL> SY.

  • Query--please help

    Hello Forum Members,
    Can you please help me out:
    SQL> select * from quarter_test4;
    QUARTER CONFIG REP_DATE
    Q1-2007 10 12-JAN-07
    Q2-2007 10 21-APR-07
    Q3-2007 870 14-AUG-07
    Q4-2007 50 15-NOV-07
    Q1-2008 60 02-JAN-09
    Q4-2006 160 02-DEC-06
    I want the following out put:
    Please note that the future quarters should have current quarter sum(config).Please note that The current table
    does not hold future quarters.I have to genarate query on which a report is based.
    My Query:
    SELECT quarter,
    CASE
    WHEN qtr > TRUNC(SYSDATE, 'q')
    THEN LAST_VALUE(sum_config IGNORE NULLS) OVER(ORDER BY qtr)
    ELSE sum_config
    END sum_config
    FROM (SELECT qtr, q.qtrstr quarter, SUM(qt.config) sum_config
    FROM (SELECT 'Q' || TO_CHAR(qtr, 'q-yyyy') qtrstr, qtr
    FROM (SELECT ADD_MONTHS(ADD_MONTHS(TRUNC(SYSDATE, 'q'), -13), 3 *(LEVEL - 1)) qtr
    FROM DUAL
    CONNECT BY LEVEL <= 9)
    ORDER BY qtr) q,
    quarter_test4 qt
    WHERE qt.quarter(+) = q.qtrstr
    GROUP BY qtr, q.qtrstr)
    ORDER BY qtr;
    Correct Output Generated:
    QUARTER SUM_CONFIG
    Q4-2006 160
    Q1-2007 10
    Q2-2007 10
    Q3-2007 870
    Q4-2007 50
    Q1-2008 60
    Q2-2008 60
    Q3-2008 60
    Q4-2008 60
    ====================================================================================================================================
    New Requirement:Product column has been added.
    SQL> select * from quarter_test3;
    QUARTER CONFIG REP_DATE PRODUCT
    Q1-2007 10 12-JAN-07 P1
    Q2-2007 10 21-APR-07 P1
    Q3-2007 870 14-AUG-07 P1
    Q4-2007 50 15-NOV-07 P1
    Q1-2008 60 02-JAN-09 P1
    Q4-2006 160 02-DEC-06 P1
    Q4-2006 997 02-DEC-06 P2
    Q4-2007 60 14-NOV-07 P2
    Q3-2007 970 14-NOV-07 P2
    Q2-2007 20 21-APR-07 P2
    Q1-2007 20 12-JAN-07 P2
    QUARTER CONFIG REP_DATE PRODUCT
    Q1-2008 70 12-JAN-08 P2
    Expected Output:
    Q4-2006 160 P1
    Q1-2007 10 P1
    Q2-2007 10 P1
    Q3-2007 870 P1
    Q4-2007 50 P1
    Q1-2008 60 P1
    Q2-2008 60 P1
    Q3-2008 60 P1
    Q4-2008 60 P1
    Q4-2006 260 P2
    Q1-2007 20 P2
    Q2-2007 20 P2
    Q3-2007 970 P2
    Q4-2007 60 P2
    Q1-2008 70 P2
    Q2-2008 70 P2
    Q3-2008 70 P2
    Q4-2008 70 P2
    My Query:
    SELECT quarter,product,
    CASE
    WHEN qtr > TRUNC(SYSDATE, 'q')
    THEN LAST_VALUE(sum_config IGNORE NULLS) OVER(ORDER BY qtr)
    ELSE sum_config
    END sum_config
    FROM (SELECT qtr, q.qtrstr quarter,product, SUM(qt.config) sum_config
    FROM (SELECT 'Q' || TO_CHAR(qtr, 'q-yyyy') qtrstr, qtr
    FROM (SELECT ADD_MONTHS(ADD_MONTHS(TRUNC(SYSDATE, 'q'), -13), 3 *(LEVEL - 1)) qtr
    FROM DUAL
    CONNECT BY LEVEL <= 9)
    ORDER BY qtr) q,
    quarter_test3 qt
    WHERE qt.quarter(+) = q.qtrstr
    GROUP BY qtr,product, q.qtrstr)
    ORDER BY substr(qtr,-4),substr(QTR,2,1)
    Output:
    QUARTER PRODUCT SUM_CONFIG
    Q4-2006 P1 160
    Q4-2006 P2 997
    Q4-2007 P1 50
    Q4-2007 P2 60
    Q4-2008 70
    Q2-2007 P1 10
    Q2-2007 P2 20
    Q2-2008 70
    Q3-2007 P1 870
    Q3-2007 P2 970
    Q3-2008 70
    QUARTER PRODUCT SUM_CONFIG
    Q1-2007 P1 10
    Q1-2007 P2 20
    Q1-2008 P1 70
    Q1-2008 P2 70
    The query had not generated q2-2008,q3-2008,q4-2008 values [60]for P1...it had only generated q2-2008,q3-2008,q4-2008 values[70] for product P2.
    Can you please advise me.

    You can make up the data using the model clause:
    SQL> create table quarter_test3 (quarter, config, product)
      2  as
      3  select 'Q1-2007', 10, 'P1' from dual union all
      4  select 'Q2-2007', 10, 'P1' from dual union all
      5  select 'Q3-2007', 870, 'P1' from dual union all
      6  select 'Q4-2007', 50, 'P1' from dual union all
      7  select 'Q1-2008', 60, 'P1' from dual union all
      8  select 'Q4-2006', 160, 'P1' from dual union all
      9  select 'Q4-2006', 997, 'P2' from dual union all
    10  select 'Q1-2007', 60, 'P2' from dual union all
    11  select 'Q2-2007', 970, 'P2' from dual union all
    12  select 'Q3-2007', 20, 'P2' from dual union all
    13  select 'Q4-2007', 20, 'P2' from dual union all
    14  select 'Q1-2008', 70, 'P2' from dual
    15  /
    Tabel is aangemaakt.
    SQL> select 'Q' || to_char(mod(q,4) + 1) || '-' || to_char(trunc(q/4)) quarter
      2       , config
      3       , product
      4    from quarter_test3
      5   model
      6         partition by (product)
      7         dimension by (to_number(substr(quarter,4))*4 + to_number(substr(quarter,2,1)) - 1 q)
      8         measures (config)
      9         rules
    10         ( config[for q from 2006*4 + 3 to 2008*4 + 3 increment 1]
    11           = nvl(config[cv()],config[cv()-1])
    12         )
    13   order by product
    14       , q
    15  /
    QUARTER                                                                                CONFIG PR
    Q4-2006                                                                                   160 P1
    Q1-2007                                                                                    10 P1
    Q2-2007                                                                                    10 P1
    Q3-2007                                                                                   870 P1
    Q4-2007                                                                                    50 P1
    Q1-2008                                                                                    60 P1
    Q2-2008                                                                                    60 P1
    Q3-2008                                                                                    60 P1
    Q4-2008                                                                                    60 P1
    Q4-2006                                                                                   997 P2
    Q1-2007                                                                                    60 P2
    Q2-2007                                                                                   970 P2
    Q3-2007                                                                                    20 P2
    Q4-2007                                                                                    20 P2
    Q1-2008                                                                                    70 P2
    Q2-2008                                                                                    70 P2
    Q3-2008                                                                                    70 P2
    Q4-2008                                                                                    70 P2
    18 rijen zijn geselecteerd.Regards,
    Rob.

  • Query  -  please help - Urgent

    Hi,
    Currently we have a query (Created on Multicube - one is sales plan cube and another is Sales actuals).
    When we run the query on a particular day (example: 7/10/2006), the report looks like as below:
    Cal.Year/Month       Sales plan     Sales Actuals
    Jan'06               310             305
    Feb'06               280             277
    Mar'06               310             309
    Apr'06               300             300
    MAy'06               310             305
    June'06              300             305
    July'06              310             90
    The current report shows the whole month's Sales Plan (July'06 - 310) with Actual Sales for 9 days(for July'06 - 90). Whereas, We want to compare the sales Plan (first 9 days of the month) against the sales actuals for 9 days.
    We do not have 0calday in both the cubes. Please help.
    Thanks,

    David,
    As rightly pointed out , you cannot get the plan value for 9 days if you do not have 0calday. However some things you can do...
    1. Have a virtual KF which gets the number of days from the current calendar day ( system date ) to the first date of the month - this can be written with a simple exit since the first date is fixed.
    2. calculate the plan value from that - pro rated plan value = plan value for month / number of days in month * number of days elapsed. If need be have the virtual KF return the ratio of number of days elapsed/number of days in the month
    3. Now you can do the comparison assuming that the value of actuals will be till the current date and not beyond.
    This is assuming that you can write a Virtual KF and the number of records is less enough to avoid any performance issues
    Hope it helps..
    Arun
    Assign points if helpful
    Message was edited by: Arun Varadarajan

  • PROBLEM WITH HIERARCHICAL QUERY - PLEASE HELP

    I have got three tables :
    CREATE TABLE FIRM
    (FID INTEGER NOT NULL PRIMARY KEY,
    FNAME VARCHAR(40),
    FTYPE VARCHAR(3),
    MASTERID INTEGER );
    CREATE TABLE FACULTY
    (FAID INTEGER NOT NULL PRIMARY KEY,
    FANAME VARCHAR(40),
    FATYPE VARCHAR(3),
    MASTERID INTEGER );
    CREATE TABLE EMPLOYEE
         (EID INTEGER NOT NULL PRIMARY KEY,
    ENAME VARCHAR(20),
    ESURNAME VARCHAR(20),
         EJOB VARCHAR(3),
         MASTERID INTEGER );
    This is a hierarchical tree(or is ment to be, I,m complete rookie ) . Firm can be the root or can be slave to another firm. Faculty can be slave to firm, or to another faculty. Employee can be slave to faculty or to another employee(e.g. boss). This connections are specified by MASTERIDs.
    I need to write a procedure, which parameter would be node ID. It is meant to create a VIEW from this node as if it was a root (view of a subtree).
    I tried CONNECT BY clause but it works only on one table at a time and I have here three tables.
    I completely don,t know how to write it. Please help.

    create view hierarchy as
    select id, master_id, name from table1
    union all
    select id, master_id, name from table2
    union all
    select id, master_id, name from table3
    Then do your connect by query against hierarchy.
    It will not work in 8i (connect by on views not allowed), so you will need to materialize the view.
    Kirill

Maybe you are looking for

  • XML, PHP and Flash forms

    After searching several tutorials and forum posts, I still am puzzled at how to get started on this. I want to enable my client to upload changes to the XML file used on his site without knowing anything about XML. He also wants to upload images whic

  • Using a 1080p display witha power mac?

    my cinema display was stolen.  i bought an adaptor and hooked the dvi port to the hdmi on my cony 1080p lcd (kdl s5100).  the resolution seems to have "cut the crusts off my sandwich".  is there a fix for this, so i can see my sidebars and menus?

  • Updating the PO

    Hi All,      when we save the transaction QM02, we need to update the PO, based on status of the Notification Number. status is not available  in any  userexit/BADI. How can i resolve the problem?. is it possible to trigger the custom program, when t

  • BDC - Uplaod material master

    hi, i want to upload updation & deletetion of  unit of measurment in material master through BDC. after that client require repot for that , how many are updated or deleted, clent is asking Transactin code also. which T code i will suggest and which

  • Homogeneous System Copy - Remaining ALE Configuration

    We made a system copy of an R/3 HR productive system to a new server for testing purpose. The system copy was made with a backup / restore procedure. Now, the new server in online and is working fine. The problem starts when we want to make the accou