Analytical function need to work based on previous rows result.

My data are sorted based on sartdatetime column.but i need the sum(value) based on below condition.
Condition :
i need to check with current row with previous rows
if they are same then i need to sum the value with previous row.
else i need to start the summation.
My data like below:
Rownum     Code     Sartdatetime     Value
1     4619     12/9/2012 11:00      219
2     5344     12/9/2012 14:39      27
3     4619     12/9/2012 15:11     20
4     4619     12/9/2012 19:33     14
5     4619     12/9/2012 20:53      16
6     6851     12/9/2012 21:21      1
7     4619     12/9/2012 21:22     35
8     5623     12/10/2012 1:59     4
9     5623     12/10/2012 2:03 3
10     5623     12/10/2012 2:06 5
11     4619     12/10/2012 2:17 1
12     5623     12/10/2012 2:18     5
13     5623     12/10/2012 2:25      2
14     5623     12/10/2012 2:27 2
15     4619     12/10/2012 2:29      30
Eg:
Take the first row. For that no previous rows. So we directly added the value like below.
Rownum     Code     Sartdatetime     Value sum(val)
1     4619     12/9/2012 11:00      219 219
take the seconds row, we can check the code value (5344)with first row value (4619). the values are different . So our summation like.
2     5344     12/9/2012 14:39      27 27
for third row , we can check the code value (4619)with second row value (5344). the values are different . So our summation like.
3     4619     12/9/2012 15:11     20 20
for fourth row we can check the code value (4619)with second row value (4619). the values are same. So our summation like.
4     4619     12/9/2012 19:33     14 34 - The sum of previous row result.
5 th row also same value so
5     4619     12/9/2012 20:53      16 50T he sum of previous row result.
then the same value come again in 7 th row so here we need to start the summation like below.
7     4619     12/9/2012 21:22     35 35.
i want the result like below.
Rownum     Code     Sartdate time     Value Expected result
1     4619     12/9/2012 11:00      219 219
2     5344     12/9/2012 14:39      27 27
3     4619     12/9/2012 15:11     20 20
4     4619     12/9/2012 19:33     14 34
5     4619     12/9/2012 20:53      16 50
6     6851     12/9/2012 21:21      1 1
7     4619     12/9/2012 21:22     35 35
8     5623     12/10/2012 1:59     4 4
9     5623     12/10/2012 2:03 3 7
10     5623     12/10/2012 2:06 5 12
11     4619     12/10/2012 2:17 1 1
12     5623     12/10/2012 2:18     5 5
13     5623     12/10/2012 2:25      2 7
14     5623     12/10/2012 2:27 2 9
15     4619     12/10/2012 2:29      30 30
Please help

Hi,
Welcome to the forum!
Here's one way to do what you want:
WITH     got_grp_id     AS
     SELECT  code, starttime, value
     ,     ROW_NUMBER () OVER ( ORDER BY      starttime )     AS r_num
     ,     ROW_NUMBER () OVER ( ORDER BY      starttime )
           - ROW_NUMBER () OVER ( PARTITION BY  code
                                     ORDER BY          starttime
                       )                    AS grp_id
     FROM    table_x
SELECT       r_num
,       code
,       starttime
,       value
,       SUM (value) OVER ( PARTITION BY  code, grp_id
                                ORDER BY          starttime
                  )         AS sum_value
FROM      got_grp_id
ORDER BY  starttime
;For an explanation of the Fixed Difference technique, see {message:id=9953384} and/or {message:id=9957164}
Whenever you have a problem, please post CREATE TABLE and INSERT statements for your same data. For example:
CREATE TABLE     table_x
(   code     NUMBER (4)
,   starttime     DATE          UNIQUE
,   value     NUMBER
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/9/2012 11:00', 'MM/DD/YYYY HH24:MI'),
                                        219);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5344, TO_DATE ( '12/9/2012 14:39', 'MM/DD/YYYY HH24:MI'),
                                        27);
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/9/2012 15:11', 'MM/DD/YYYY HH24:MI'),
                                        20);
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/9/2012 19:33', 'MM/DD/YYYY HH24:MI'),
                                        14);
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/9/2012 20:53', 'MM/DD/YYYY HH24:MI'),
                                        16);
INSERT INTO table_x (code, starttime, value)
       VALUES         (6851, TO_DATE ( '12/9/2012 21:21', 'MM/DD/YYYY HH24:MI'),
                                        1);
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/9/2012 21:22', 'MM/DD/YYYY HH24:MI'),
                                        35);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5623, TO_DATE ( '12/10/2012 1:59', 'MM/DD/YYYY HH24:MI'),
                                        4);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5623, TO_DATE  ('12/10/2012 2:03' , 'MM/DD/YYYY HH24:MI' ),
                                                   3);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5623, TO_DATE ( '12/10/2012 2:06', 'MM/DD/YYYY HH24:MI'),
                                        5);
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/10/2012 2:17', 'MM/DD/YYYY HH24:MI'),
                                        1);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5623, TO_DATE ( '12/10/2012 2:18', 'MM/DD/YYYY HH24:MI'),
                                        5);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5623, TO_DATE ( '12/10/2012 2:25', 'MM/DD/YYYY HH24:MI'),
                                        2);
INSERT INTO table_x (code, starttime, value)
       VALUES         (5623, TO_DATE  ('12/10/2012 2:27' , 'MM/DD/YYYY HH24:MI' ),
                                                   2);
INSERT INTO table_x (code, starttime, value)
       VALUES         (4619, TO_DATE ( '12/10/2012 2:29', 'MM/DD/YYYY HH24:MI'),
                                        30);
COMMIT; See the forum FAQ {message:id=9360002}
Output:
R_NUM       CODE STARTTIME             VALUE  SUM_VALUE
    1       4619 12/9/2012 11:00         219        219
    2       5344 12/9/2012 14:39          27         27
    3       4619 12/9/2012 15:11          20         20
    4       4619 12/9/2012 19:33          14         34
    5       4619 12/9/2012 20:53          16         50
    6       6851 12/9/2012 21:21           1          1
    7       4619 12/9/2012 21:22          35         35
    8       5623 12/10/2012 01:59          4          4
    9       5623 12/10/2012 02:03          3          7
   10       5623 12/10/2012 02:06          5         12
   11       4619 12/10/2012 02:17          1          1
   12       5623 12/10/2012 02:18          5          5
   13       5623 12/10/2012 02:25          2          7
   14       5623 12/10/2012 02:27          2          9
   15       4619 12/10/2012 02:29         30         30Edited by: Frank Kulash on Dec 24, 2012 6:44 AM
Added sample data.

Similar Messages

  • Analytic Functions - Need resultset only in one select

    Hello Experts,
    Problem Definition: Using Analytic Function, get Total sales for the Product P1 and Customer C1 [Total sales for the customer itself] in one line. I want to restrict the ResultSet of the query to Product P1, please look at the data below, queries and problems..
    Data
    Customer Product Qtr Sales
    C1 P1 19991 100.00
    C1 P1 19992 125.00
    C1 P1 19993 175.00
    C1 P1 19994 300.00
    C1 P2 19991 100.00
    C1 P2 19992 125.00
    C1 P2 19993 175.00
    C1 P2 19994 300.00
    C2 P1 19991 100.00
    C2 P1 19992 125.00
    C2 P1 19993 175.00
    C2 P1 19994 300.00
    Problem, I want to display....
    Customer Product ProdSales CustSales
    C1 P1 700 1400
    But Without using outer query, i.e. please look below for the query that returns this reult with two select, I want this result in one query only..
    Select * From ----*** want to avoid this... ***----
    (Select Customer,Product,
    Sum(Sales) ProdSales,
    Sum(Sum(Sales)) Over(Partition By Customer) CustSales
    From t1
    Where customer='C1')
    Where
    Product='P1' ;
    Also, I want to avoid Hard coding of P1 in the select clause....
    I mean, I can do it in one shot/select, but look at the query below, it uses P1 in the select clause, which is No No!! P1 is allowed only in Where or Having ..
    Select Customer,Decode(Product, 'P1','P1','P1') Product,
    Decode(Product,'P1',Sales,0) ProdSales,
    Sum(Sum(Sales)) Over (Partition By Customer ) CustSales
    From t1
    Where customer='C1' ;
    This will get me what I want, but as I said earlier, I want to avoid using P1 in the
    Select clause..
    Goal is to Avoid using
    1-> Two Select/Outer Query/In Line Views
    2-> Product 'P1' in the Select clause...No hard coded product name in the select clause and group by clause..
    Thanks
    -Dhaval

    Select * From ----*** want to avoid this... ***----
    (Select Customer,Product,
    Sum(Sales) ProdSales,
    Sum(Sum(Sales)) Over(Partition By Customer)
    CustSales
    From t1
    Where customer='C1')
    Where
    Product='P1' ;
    Goal is to Avoid using
    1-> Two Select/Outer Query/In Line ViewsWhy?

  • Finding all rows based on previous rows column value diffrence

    I have an interesting requirement. There is a DATE column and a user in a table and I have to find all rows for all the user for which the previous row and current row has time difference of lets say more than 30 minutes. Rows are already sorted by time.
    For example in the following table, we need ID 4 and 6 for user 1.
    ID User date
    1 1 today 1 hour 0 min 0 s
    2 1 today 1 hour 1 min 0 s
    3 1 today 1 hour 29 min 0 s
    *4 1 today 1 hour 59 min 3 s*
    5 1 today 2 hour 10 min 2 s
    *6 1 today 2 hour 50 min 7 s*
    Edited by: user733179 on Mar 5, 2009 12:00 PM

    Hi,
    To get all the users in the result set, outer-join to a table (or subquery, as shown bleow) that has one row per user:
    WITH     got_dif     AS
         SELECT     id,     dt,     usr
         ,     (dt - LAG (dt)
                    OVER ( PARTITION BY  usr
                              ORDER BY          dt
              )             -- difference in days
                  * 24 * 60  AS minutes_dif
         FROM     table_x
    --     WHERE     ...     -- if needed
    ,     all_users     AS
         SELECT DISTINCT  usr
         FROM              table_x
    SELECT     gd.id,     gd.dt,     au.usr
    ,     NVL2 ( gd.usr
              , 'Okay'
              , 'Dif is never > 30'
              )    AS flag
    FROM          all_users
    LEFT OUTER JOIN     got_dif          ON  gd.usr         = au.usr
                                AND gd.minutes_dif > 30
    ;Notice that the test for a 30-minute difference is part of the join condition.
    You don't need the flag column, and you may not want it.
    If you would prefer a separate query of just the users that are not in the original result set, then you can use the original query (modified only slightly) as an EXISTS ior NOT IN sub-query. For example:
    SELECT DISTINCT     usr
    FROM    table_x
    WHERE   usr  NOT IN
         WITH     got_dif     AS
              SELECT     id,     dt,     usr
              ,     (dt - LAG (dt)
                             OVER ( PARTITION BY  usr
                                      ORDER BY          dt
                    )             -- difference in days
                        * 24 * 60  AS minutes_dif
               FROM     table_x
    --          WHERE     ...     -- if needed
         SELECT  usr   -- Only 1 column in SELECT clause
         FROM     got_dif
         WHERE     minutes_dif     > 30
    ;

  • How does the analytical function RANK OVER() work inoracle??

    Hi,
    Can i get the information about how oracle internally handles RANK over() ?

    http://tahiti.oracle.com
    http://asktom.oracle.com

  • Populate other column value based on previous row value using t-sql

    Hi All,
    I have one table with 6 columns, let say ID1, ID2,status, EnteredDate,NewValue, Old Value. Where ID1 is the primary key field
     ID1       ID2       status             EnteredDate        NewValue      
    Old Value
      1          XYZ       New              07/12/2012           
    ABC               null
      2          XYZ       Renewal        08/19/2012            DEF               
    null
      3          XYZ       Cancel           10/21/2012            GHI               
    null
      4          ZYX       New              09/15/2012           
    BDF               null
      5          ZYX       Cancel           10/21/2012            MNS             
    null
      6          MBS       New              05/29/2012           
    EXP               null
      7          SBX        New              05/29/2012           
    SKS               null
      8          SBX        Renewal        06/21/2012            QSR              
    SKS
    Basically I need a sql query which should populate Output as below. Status=New will always have old date compared to Renewal and Cancel and also OldValue field will be null always for status=New
    Output:
     ID1       ID2       status           EnteredDate        NewValue      
    Old Value     Row_Num(based on ID1,ID2,Entereddate)
      1          XYZ       New              07/12/2012           
    ABC               null                 1
      2          XYZ       Renewal        08/19/2012            DEF               
    ABC                2
      3          XYZ       Cancel           10/21/2012            GHI               
    DEF                 3
      4          ZYX       New              09/15/2012           
    BDF               null                   1
      5          ZYX       Cancel           10/21/2012            MNS              
    BDF                 2
      6          MBS       New              05/29/2012           
    EXP               null                  1
      7          SBX        New              05/29/2012           
    SKS               null                  1
      8          SBX        Renewal        06/21/2012            QSR              
    SKS                2
    Thanks in Advance, its very urgent. Pls send me the query ASAP.
    RH
    sql

    Hi,
    In case of you are using SQL 2012, you can use new built-in function like LAG, try this;
    USE tempdb
    GO
    CREATE TABLE dbo.Test
    ID1 int PRIMARY KEY
    , ID2 char(3) NOT NULL
    , Status varchar(20) NOT NULL
    , EnteredDate date NOT NULL
    , NewValue char(3) NOT NULL
    , OldValue char(3) NULL
    GO
    INSERT INTO dbo.Test
    (ID1, ID2, Status, EnteredDate, NewValue, OldValue)
    VALUES
    (1, 'XYZ', 'New', '07/12/2012', 'ABC', null)
    , (2, 'XYZ', 'Renewal', '08/19/2012', 'DEF', null)
    , (3, 'XYZ', 'Cancel', '10/21/2012', 'GHI', null)
    , (4, 'ZYX', 'New', '09/15/2012' ,'BDF', null)
    , (5, 'ZYX', 'Cancel', '10/21/2012', 'MNS',null)
    , (6, 'MBS', 'New', '05/29/2012', 'EXP', null)
    , (7, 'SBX', 'New', '05/29/2012', 'SKS', null)
    , (8, 'SBX', 'Renewal', '06/21/2012', 'QSR', 'SKS')
    WITH cte
    AS
    (SELECT ID1, ID2, Status, EnteredDate, NewValue, OldValue
    , ROW_NUMBER() OVER(PARTITION BY ID2 ORDER BY ID2) Row_Num
    , LAG(NewValue, 1, 0) OVER(PARTITION BY ID2 ORDER BY ID2) NewOldValue
    FROM dbo.Test)
    SELECT ID1, ID2, Status, EnteredDate, NewValue
    , NULLIF(NewOldValue, '0') NewOldValue, Row_Num
    FROM cte
    ORDER BY ID1, ID2;
    Dinesh Priyankara
    http://dinesql.blogspot.com/
    Please use Mark as answer (Or Propose as answer) or Vote as helpful if the post is useful.

  • Add row based on previous row in table control?

    Dear all,
    I have a table control with some rows. Every row contains one button. On button click i want to add another row with dirrerent data. I want to add content based on button text or another columns (ex text views text,) based on this text view, I have to add row. One button can click any no of times. On every click i want to add row, but desired position and content should be based on button click.  Any help in doing this??
    Cheers,
    Venkys.

    Refer to these old threads referring this table and button problem.
    Adding rows to table
    How to create different rows in table or in ALV?
    and for the current scenario what you can do is ...
    in the eventhandler of the button click .
    find out the row number by using the code to read index.
    then based upon that add the element in the node at the desired position.
    finding the row number
      data indx type i.
          DATA lo_el TYPE REF TO if_wd_context_element.
          lo_el = wdevent->get_context_element( 'CONTEXT_ELEMENT' ).
    indx = lo_el->get_index( ).
    and the adding the element at the desired position say n
        DATA lo_nd_zdealer TYPE REF TO if_wd_context_node.
        lo_nd_zdealer = wd_context->get_child_node( name = 'DEALER' ).
    data ls_str type  wd_this->element_dealer.
       ls_str-id = '00023445'.
       ls_str-name = 'sarbjeet'.
       ls_str-location = 'hosiarpur'.
       ls_str-status = '0001'.
       lo_nd_zdealer->bind_structure( new_item = ls_str
       set_initial_elements = abap_false
       index = n
    thanks
    sarbjeet singh

  • As Needed Not Working. Getting all rows on page open.

    Using JDev 9.0.3.1 I have a simple viewObject that is flagged for retrieve data "As Needed" along with a Fetch size of 100 and Maximum Fetch = -1
    I have a .uix page with selectable search parameters on top of the page and the VO on the second half of the page.
    The problem: All data is retrieved and displayed when the page is openned. I tried all different settings for the VO but it still does this. I don't want any rows retrieved until the "Search" button is selected.
    Any Idea? Is this a bug? Let me know.
    Thanks for your time.

    You can make use of an event named null to init you viewobject.In the event,
    1.get the viewobject;
    2.set the viewobject whereclause
    In UIX:
    <event name="null">
    <bc4j:findRootAppModule name="VTbReceiveListView1AppModule">
    <!-- establish the ViewObject scope -->
    <bc4j:findViewObject name="VTbReceiveListView1">
    <method class="propackage.ReceiveListAffirm" method="initReceiveList"/>
    </bc4j:findViewObject>
    </bc4j:findRootAppModule>
    </event>
    In Java code:
    public static EventResult initReceiveList(
    BajaContext context,
    Page page,
    PageEvent event) throws Throwable
    String sWhere="";
    ViewObject voThis = ServletBindingUtils.getViewObject(context);
    if (voThis == null)
    return null;
    sWhere = ...;
    voThis.setWhereClause(sWhere);
    voThis.executeQuery();
    EventResult result = new EventResult(page);
    return result;

  • Query! calculation based on previous row value

    Hi,
    ID     Code     Direction     From Amount  To Perct      To Amount
    98     POI     F          5457.00          0     
    77     LKJ     T          0          50      (5457*(50/100))
    56     MNB     T          0          25      (5457*(25/100))How to calculate 'To Amount' with in the select query? To Amount will be calculated on the basis of
    From Amount of the First row multiplied by 'To Perct'
    When I wrote a select statement it's taking 0 as 'From Amount'( Because it's in current row) and giving me 0 as 'To Amount'
    Thanks

    One possibility:
    with t as (
    select 98 id,     'POI' Code,     'F' Direction, 5457.00 FromAmount,          0 ToPerct from dual     union all
    select 77,     'LKJ',     'T',          0,          50 from dual     union all
    select 56,     'MNB',     'T',          0,          25 from dual)
    select id, code, direction, FromAmount, ToPerct,
           (tmax.MaxAmount)*(ToPerct/100) ToAmount 
    from t,
         (select max(FromAmount) MaxAmount from t) tmax ;Results:
            ID COD D FROMAMOUNT    TOPERCT   TOAMOUNT
            98 POI F       5457          0          0
            77 LKJ T          0         50     2728,5
            56 MNB T          0         25    1364,25Regards,
    Miguel

  • Wired problem with LEAD Analytical function

    I am having wired problem with LEAD Analytical function. It works fine as a standalone query
    but when used in sub-query it is not behaving as expected (explained below). I unable to troubleshoot
    the issue. Please help me.
    Detail explanation and the data follows:
    ========================= Table & Test Data =======================================
    CREATE TABLE "TESTSCHED"
    (     "SEQ" NUMBER NOT NULL ENABLE,
         "EMPL_ID" NUMBER NOT NULL ENABLE,
         "SCHED_DT" DATE NOT NULL ENABLE,
         "START_DT_TM" DATE NOT NULL ENABLE,
         "END_DT_TM" DATE NOT NULL ENABLE,
         "REC_STAT" CHAR(1) NOT NULL ENABLE,
         CONSTRAINT "TESTSCHED_PK" PRIMARY KEY ("SEQ")     
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (1, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (2, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 11:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (3, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (4, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:15:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (5, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:15:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (6, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (7, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (8, 39609, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 11:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (9, 118327, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (10, 120033, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('04-03-2008', 'dd-mm-yyyy'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (11, 120033, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('04-03-2008', 'dd-mm-yyyy'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (12, 120033, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (13, 120033, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (14, 126690, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 18:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 21:15:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (15, 126690, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 12:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 16:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (16, 126690, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 12:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 21:15:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (17, 126690, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 16:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 18:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (18, 126690, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 16:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 18:45:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (19, 169241, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 05:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (20, 169241, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 05:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (21, 169241, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (22, 169241, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (23, 200716, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (24, 200716, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (25, 200716, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (26, 252836, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (27, 252836, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (28, 252836, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (29, 252836, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 17:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 19:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (30, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 10:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (31, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 10:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (32, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 05:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (33, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:15:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 10:35:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (34, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:15:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 10:35:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (35, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 05:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 14:00:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (36, 260774, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 07:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 10:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (37, 289039, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 16:30:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:15:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (38, 289039, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 13:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 22:15:00', 'dd-mm-yyyy hh24:mi:ss'), 'I');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (39, 289039, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 13:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 16:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'A');
    insert into testsched (SEQ, EMPL_ID, SCHED_DT, START_DT_TM, END_DT_TM, REC_STAT)
    values (40, 289039, to_date('03-03-2008', 'dd-mm-yyyy'), to_date('03-03-2008 13:45:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('03-03-2008 16:30:00', 'dd-mm-yyyy hh24:mi:ss'), 'U');
    ========================= Problem Statement =======================================
    Problem Statement:
    Select all employees whose active schedule (rec_stat = 'A') start date & time falls
    between "16:30" and "17:45".
    An employee may be having multiple schedules on a day. It may be continuous or may be
    having breaks in between the schedules.
    If an employee schedule is continuous then the minimum schedule start date & time is
    considered as the start date & time of the work.
    If an employee is having breaks in the schedule, then each schedule start date & time is
    considered as start date & time.
    e.g:-1
    1     39609     2008/03/03     2008/03/03 07:00:00     2008/03/03 11:00:00     A
    2     39609     2008/03/03     2008/03/03 11:00:00     2008/03/03 11:30:00     A
    In above case, the employee schedule is continuous. The start date & time is 2008/03/03 07:00:00
    e.g:-2
    19     169241     2008/03/03     2008/03/03 05:30:00     2008/03/03 14:00:00     A
    21     169241     2008/03/03     2008/03/03 17:30:00     2008/03/03 22:00:00     A
    In above case, the employee is having a break in between. Then both records
    start date & time (i.e. 2008/03/03 05:30:00, 2008/03/03 05:30:00 ) is considered
    as start date & time.
    e.g:-3
    30     260774     2008/03/03     2008/03/03 07:00:00     2008/03/03 10:30:00     A
    31     260774     2008/03/03     2008/03/03 10:30:00     2008/03/03 14:00:00     A
    32     260774     2008/03/03     2008/03/03 05:30:00     2008/03/03 07:00:00     A
    In above case, the employee schedule is continuous. The start date & time is 2008/03/03 05:30:00
    ========================= Query to test Individual Employees =======================================
    Query-1: Test query to see the CASE and LEAD function works fine. If "CAL = 0" means that all
    schedules are continuous and the employee needs to ignored.
    SELECT fw.*,
    CASE WHEN LEAD(FW.END_DT_TM, 1)
    OVER(ORDER BY FW.START_DT_TM DESC ) IS NULL AND
    fw.start_dt_tm BETWEEN
    TO_DATE('03/03/2008 16:30:00','mm/dd/yyyy hh24:mi:ss') AND
    TO_DATE('03/03/2008 17:45:00','mm/dd/yyyy hh24:mi:ss')
    THEN Fw.seq
    WHEN LEAD(FW.END_DT_TM, 1)
    OVER(ORDER BY FW.START_DT_TM DESC) != FW.START_DT_TM AND
    fw.start_dt_tm BETWEEN
    TO_DATE('03/03/2008 16:30:00','mm/dd/yyyy hh24:mi:ss') AND
    TO_DATE('03/03/2008 17:45:00','mm/dd/yyyy hh24:mi:ss')
    THEN Fw.seq
    ELSE 0
    END AS "CAL"
    FROM TESTSCHED fw
    WHERE fw.empl_id = 289039
    and fw.rec_stat = 'A'
    Query Results
    SEQ     EMPL_ID     SCHED_DT     START_DT_TM          END_DT_TM          REC_STAT     CAL
    37     289039     2008/03/03     2008/03/03 16:30:00     2008/03/03 22:15:00     A          0
    39     289039     2008/03/03     2008/03/03 13:45:00     2008/03/03 16:30:00     A          0
    Since all "CAL" column is zero, the employee should be ignored.
    ========================= Query to be used in the application =======================================
    Query-2: Actual Query executed in the application which is not working. Query-1 is used as subquery
    here.
    SELECT
    F1.SEQ,
    F1.EMPL_ID,
    F1.SCHED_DT,
    F1.START_DT_TM,
    F1.END_DT_TM,
    F1.REC_STAT
    FROM TESTSCHED F1
    WHERE F1.SEQ IN
    (SELECT CASE
    WHEN LEAD(FW.END_DT_TM, 1)
    OVER(ORDER BY FW.START_DT_TM DESC) IS NULL AND
    FW.START_DT_TM BETWEEN
    TO_DATE('03/03/2008 16:30:00', 'mm/dd/yyyy hh24:mi:ss') AND
    TO_DATE('03/03/2008 17:45:00', 'mm/dd/yyyy hh24:mi:ss') THEN FW.SEQ
    WHEN LEAD(FW.END_DT_TM, 1)
    OVER(ORDER BY FW.START_DT_TM DESC) != FW.START_DT_TM AND
    FW.START_DT_TM BETWEEN
    TO_DATE('03/03/2008 16:30:00', 'mm/dd/yyyy hh24:mi:ss') AND
    TO_DATE('03/03/2008 17:45:00', 'mm/dd/yyyy hh24:mi:ss') THEN FW.SEQ
    ELSE 0
    END
    FROM TESTSCHED FW
    WHERE FW.EMPL_ID = F1.EMPL_ID
    AND FW.REC_STAT = 'A')
    Query-2 Results
    SEQ     EMPL_ID     SCHED_DT     START_DT_TM          END_DT_TM          REC_STAT
    9     118327     2008/03/03     2008/03/03 17:30:00     2008/03/03 22:00:00     A
    12     120033     2008/03/03     2008/03/03 17:30:00     2008/03/03 19:30:00     A
    17     126690     2008/03/03     2008/03/03 16:45:00     2008/03/03 18:45:00     A
    21     169241     2008/03/03     2008/03/03 17:30:00     2008/03/03 22:00:00     A
    24     200716     2008/03/03     2008/03/03 17:30:00     2008/03/03 22:00:00     A
    28     252836     2008/03/03     2008/03/03 17:30:00     2008/03/03 19:30:00     A
    37     289039     2008/03/03     2008/03/03 16:30:00     2008/03/03 22:15:00     A
    Problem with Query-2 Results:
    Employee IDs 126690 & 289039 should not appear in the List.
    Reason: Employee 126690: schedule start date & time is 2008/03/03 12:45:00 and falls outside
    "16:30" and "17:45"
    14     126690     2008/03/03     2008/03/03 18:45:00     2008/03/03 21:15:00     A
    17     126690     2008/03/03     2008/03/03 16:45:00     2008/03/03 18:45:00     A
    15     126690     2008/03/03     2008/03/03 12:45:00     2008/03/03 16:45:00     A
    Reason: Employee 289039: schedule start date & time is 2008/03/03 13:45:00 and falls outside
    37     289039     2008/03/03     2008/03/03 16:30:00     2008/03/03 22:15:00     A
    39     289039     2008/03/03     2008/03/03 13:45:00     2008/03/03 16:30:00     A

    Your LEAD function is order by fw.start_dt_tm, in your first sql, the where clause limited the records return to REC_STAT = A and EMPL_ID = 289039.
    In your 2nd query, there is no where clause, so to the LEAD function, the next record after seq 37 is seq 38 which has an end date of 2003-03-03 10:15 pm, not equal to the start time of record seq 37.
    Message was edited by:
    user477455

  • Analytic function to retrieve a value one year ago

    Hello,
    I'm trying to find an analytic function to get a value on another row by looking on a date with Oracle 11gR2.
    I have a table with a date_id (truncated date), a flag and a measure. For each date, I have at least one row (sometimes 2), so it is gapless.
    I would like to find analytic functions to show for each date :
    sum of the measure for that date
    sum of the measure one week ago
    sum of the measure one year ago
    As it is gapless I managed to do it the week doing a group by date in a subquery and using a LAG with offset set to 7 on top of it (see below).
    However I'm struggling on how to do that for the data one year ago as we might have leap years. I cannot simply set the offset to 365.
    Is it possible to do it with a RANGE BETWEEN window clause? I can't manage to have it working with dates.
    Week :LAG with offset 7
    SQL Fiddle
    or
    create table daily_counts
      date_id date,
      internal_flag number,
      measure1 number
    insert into daily_counts values ('01-Jan-2013', 0, 8014);
    insert into daily_counts values ('01-Jan-2013', 1, 2);
    insert into daily_counts values ('02-Jan-2013', 0, 1300);
    insert into daily_counts values ('02-Jan-2013', 1, 37);
    insert into daily_counts values ('03-Jan-2013', 0, 19);
    insert into daily_counts values ('03-Jan-2013', 1, 14);
    insert into daily_counts values ('04-Jan-2013', 0, 3);
    insert into daily_counts values ('05-Jan-2013', 0, 0);
    insert into daily_counts values ('05-Jan-2013', 1, 1);
    insert into daily_counts values ('06-Jan-2013', 0, 0);
    insert into daily_counts values ('07-Jan-2013', 1, 3);
    insert into daily_counts values ('08-Jan-2013', 0, 33);
    insert into daily_counts values ('08-Jan-2013', 1, 9);
    commit;
    select
        date_id,
        total1,
        LAG(total1, 7) OVER(ORDER BY date_id) total_one_week_ago
      from
          select
            date_id,
            SUM(measure1) total1
          from daily_counts
          group by date_id
    order by 1;
    Year : no idea?
    I can't give a gapless example, would be too long but if there is a solution with the date directly :
    SQL Fiddle
    or add this to the schema above :
    insert into daily_counts values ('07-Jan-2012', 0, 11);
    insert into daily_counts values ('07-Jan-2012', 1, 1);
    insert into daily_counts values ('08-Jan-2012', 1, 4);
    Thank you for your help.
    Floyd

    Hi,
    Sorry, I;m not sure I understand the problem.
    If you are certain that there is at least 1 row for every day, then you can be sure that the GROUP BY will produce exactly 1 row per day, and you can use LAG (total1, 365) just like you already use LAG (total1, 7).
    Are you concerned about leap years?  That is, when the day is March 1, 2016, do you want the total_one_year_ago column to reflect March 1, 2015, which was 366 days earlier?  If that case, use
    date_id - ADD_MONTHS (date_id, -12)
    instead of  365.
    LAG only works with an exact number, but you can use RANGE BETWEEN with other analytic functions, such as MIN or SUM:
    SELECT DISTINCT
              date_id
    ,         SUM (measure1) OVER (PARTITION BY date_id)    AS total1
    ,         SUM (measure1) OVER ( ORDER BY      date_id
                                    RANGE BETWEEN 7 PRECEDING
                                          AND     7 PRECEDING
                                  )                       AS total1_one_week_ago
    ,         SUM (measure1) OVER ( ORDER BY      date_id
                                    RANGE BETWEEN 365 PRECEDING
                                          AND     365 PRECEDING
                                  )                       AS total1_one_year_ago
    FROM      daily_counts
    ORDER BY  date_id
    Again, use date arithmetic instead of the hard-coded 365, if that's an issue.
    As Hoek said, it really helps to post the exact results you want from the given sample data.  You're miles ahead of the people who don't even post the sample data, though.
    You're right not to post hundreds of INSERT statements to get a year's data.  Here's one way to generate sample data for lots of rows at the same time:
    -- Put a 0 into the table for every day in 2012
    INSERT INTO daily_counts (date_id, measure1)
    SELECT  DATE '2011-12-31' + LEVEL
    ,       0
    FROM    dual
    CONNECT BY LEVEL <= 366

  • Need some kind of Analytical Function

    Hi Oracle experts
    I need a little help from you experts. I have a PARTY table as listed below
    The existing data
    Party key     ID_INTERNAL     EID          BID
    1          11111          123
    1          11111          321
    1          22222          321          899
    1          66666          ------          888
    New records comes
    I have to assign a party key to each record based on which attribute is matching
    Now the situation is as new records comes.
    New records comes
    ID_INTERNAL     EID          BID
    22222          555
    44444          555          
    89898          ------          888
    If I match on ID_INTERNAL I may not be able to match ID_INTERNAL 44444 and 89898 and if I match EID or BID the same situation.
    Is thera any analytical function which helps me assigning a party key to all the recoords. ALl the above records should be assigned PARTY KEY 1 only.
    Please help
    Thanks
    Rajesh

    Justin
    My main goal is to assign a party key from existing set of records to the new records which are being selected/inserted. I have to write my algoritum in such a way that the new values should match their value in existing records.
    Example
    my first new record has a value of 11111 under ID_INTERNAL and in the same record it has a value of 555 under EID attribute. so based on matching algoritum for ID INTERNAL it will be assigned existing party key 1.
    Similarly second new record has a value of 87777 under ID INTERNAL and has a value of 555 under EID and this ID INTERNAL does not exists in the target table. but the value of 555 is available under EID attribute so I have to write algoritum based on EID.
    Now the delima is my target table is as follows
    Party key PARTYID PARTYNAME
    1 11111 ITSID
    1 123 EID
    1 321 EID
    Now when new records come I have to write match algortium for ID_INTERNAL to PARTYID for Partyname='ITSID'
    Once matched this record ID INTERNAL=11111 and EID =555 assigned a party key=1. So after first record the output table slooks like
    Party key PARTYID PARTYNAME
    1 11111 ITSID
    1 123 EID
    1 321 EID
    1 555 EID
    Same way for second new record where the values are ID_INTERNAL=87777 and EID=555. I have to write match algortium based on EID because the EID value of 555 already exists in target tabel with party key.
    SO after second record the target table will look like
    Party key PARTYID PARTYNAME
    1 11111 ITSID
    1 123 EID
    1 321 EID
    1 555 EID
    1 87777 ITSID
    So this is how I have to solve this match algoritum.
    Please help me if you need any information I will be glad to provide you all.
    Thanks
    Regards
    Rajesh

  • Analytic function to count rows based on Special criteria

    Hi
    I have the following query with analytic function but wrong results on the last column COUNT.
    Please help me to achive the required result.Need to change the way how I select the last column.
    1)I am getting the output order by b.sequence_no column . This is a must.
    2)COUNT Column :
    I don't want the total count based on thor column hence there is no point in grouping by that column.
    The actual requirement to achieve COUNT is:
    2a -If in the next row, if either the THOR and LOC combination changes to a new value, then COUNT=1
    (In other words, if it is different from the following row)
    2b-If the values of THOR and LOC repeats in the following row, then the count should be the total of all those same value rows until the rows become different.
    (In this case 2b-WHERE THE ROWS ARE SAME- also I only want to show these same rows only once. This is shown in the "MY REQUIRED OUTPUT) .
    My present query:
    select    r.name REGION ,
              p.name PT,
              do.name DELOFF,
              ro.name ROUTE,
    decode(th.thorfare_name,'OSIUNKNOWN',NULL,th.thorfare_name)
               THOR,
             l.name LOC ,
              b.sequence_no SEQ,
               CASE WHEN th.thorfare_name = LAG (th.thorfare_name)
                OVER (order by b.sequence_no)
                or th.thorfare_name = LEAD (th.thorfare_name)
                OVER (order by b.sequence_no)
                THEN  COUNT(b.sequence_no) OVER (partition by r.name,th.thorfare_name,l.name order BY b.sequence_no
              ELSE 1
              END COUNT
    from   t_regions r,t_post_towns p,t_delivery_offices do, t_routes ro, t_counties c,t_head_offices ho,
    t_buildings b,t_thoroughfares th,t_localities l
    where   th.thorfare_id = b.thorfare_id
    and    nvl(b.invalid,'N')='N'
    and    b.route_id=ro.route_id(+)
    and    b.locality_id =l.locality_id(+)
    and    ro.delivery_office_id=do.delivery_office_id(+)
    and    do.post_town_id = p.post_town_id(+)
    and    p.ho_id=ho.ho_id(+)
    and     ho.county_id = c.county_id(+)
    and     c.region_id = r.region_id(+)
    and    r.name='NAAS'
    and    do.DELIVERY_OFFICE_id= &&DELIVERY_OFFICE_id
    and    ro.route_id=3405
    group by r.name,p.name,do.name,ro.name,th.thorfare_name,l.name,b.sequence_no
    ORDER BY ro.name,b.sequence_no;My incorrect output[PART OF DATA]:
    >
    REGION     PT DELOFF ROUTE     THOR LOC SEQ COUNT
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 1 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 2 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 4 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 5 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 THEGROVE CEL 2 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 7 3
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 8 4
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 9 5
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 10 6
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 11 7
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 12 8
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 15 2
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 19 3
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 24 4
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 29 5
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 34 6
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 39 7
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 42 2
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 43 2
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 44 3
    My required output[PART OF DATA]-Please compare with the above.:
    >
    REGION     PT DELOFF ROUTE     THOR LOC COUNT
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 THEGROVE CEL 1
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 NEWTOWNRD CEL 6
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 DUBLINRD CEL 7
    NAAS     NAAS MAYNOOTH     MAYNOOTHR010 PRIMHILL CEL 2
    NOTE :Count as 1 is correctly coming.
    But where there is same rows and I want to take the total count on them, I am not getting.
    Pls pls help.
    Thanks
    Edited by: Krithi on 04-Nov-2010 05:28

    Nicosa wrote:
    Hi,
    Can you give us some sample data (create table + inserts orders) to play with ?
    Considering your output, I'm not even sure you need analytic count.Yes sure.
    I am describing the query again here with 3 tables now to make this understand better.
    Given below are the create table statements and insert statements for these 3 tables.
    These tables are - BULDINGSV,THORV and LOCV
    CREATE TABLE BUILDINGSV
      BUILDING_ID                  NUMBER(10)       NOT NULL,
      INVALID                      VARCHAR2(1 BYTE),
      ROUTE_ID                     NUMBER(10),
      LOCALITY_ID                  NUMBER(10),
      SEQUENCE_NO                  NUMBER(4),
      THORFARE_ID                  NUMBER(10) NOT NULL
    CREATE TABLE THORV
      THORFARE_ID            NUMBER(10)             NOT NULL,
      THORFARE_NAME          VARCHAR2(40 BYTE)      NOT NULL
    CREATE TABLE LOCV
      LOCALITY_ID            NUMBER(10)             NOT NULL,
      NAME                   VARCHAR2(40 BYTE)      NOT NULL);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002372, 'N', 3405, 37382613, 5, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002363, 'N', 3405, 37382613, 57, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002362, 'N', 3405, 37382613, 56, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002360, 'N', 3405, 37382613, 52, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002358, 'N', 3405, 37382613, 1, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002240, 'N', 3405, 37382613, 6, 9002284);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002229, 'N', 3405, 37382613, 66, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002228, 'N', 3405, 37382613, 65, 35291872);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002226, 'N', 3405, 37382613, 62, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002222, 'N', 3405, 37382613, 43, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002217, 'N', 3405, 37382613, 125, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002221, 'N', 3405, 37382613, 58, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002214, 'N', 3405, 37382613, 128, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33363182, 'N', 3405, 37382613, 114, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33363185, 'N', 3405, 37382613, 115, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002371, 'N', 3405, 37382613, 2, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003329, 'N', 3405, 37382613, 415, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002359, 'N', 3405, 37382613, 15, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002224, 'N', 3405, 37382613, 61, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003318, 'N', 3405, 37382613, 411, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003326, 'N', 3405, 37382613, 412, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003327, 'N', 3405, 37382613, 413, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003328, 'N', 3405, 37382613, 414, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003330, 'N', 3405, 37382613, 416, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003331, 'N', 3405, 37382613, 417, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27003332, 'N', 3405, 37382613, 410, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27004795, 'N', 3405, 37382613, 514, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (27004807, 'N', 3405, 37382613, 515, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (59002227, 'N', 3405, 37382613, 64, 35291872);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33230805, 'N', 3405, 37382613, 44, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231027, 'N', 3405, 37382613, 7, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231058, 'N', 3405, 37382613, 9, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231078, 'N', 3405, 37382613, 10, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231087, 'N', 3405, 37382613, 11, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33231093, 'N', 3405, 37382613, 12, 9002375);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (33229890, 'N', 3405, 37382613, 55, 9002364);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561996, 'N', 3405, 34224751, 544, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561997, 'N', 3405, 34224751, 543, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561998, 'N', 3405, 34224751, 555, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562000, 'N', 3405, 34224751, 541, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562001, 'N', 3405, 34224751, 538, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562028, 'N', 3405, 35417256, 525, 0);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562031, 'N', 3405, 35417256, 518, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562032, 'N', 3405, 35417256, 519, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562033, 'N', 3405, 35417256, 523, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561939, 'N', 3405, 34224751, 551, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561940, 'N', 3405, 34224751, 552, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561941, 'N', 3405, 34224751, 553, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561942, 'N', 3405, 35417256, 536, 0);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561943, 'N', 3405, 35417256, 537, 0);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561970, 'N', 3405, 35417256, 522, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561972, 'N', 3405, 35417256, 527, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561974, 'N', 3405, 35417256, 530, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561975, 'N', 3405, 35417256, 531, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561980, 'N', 3405, 34224751, 575, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561981, 'N', 3405, 34224751, 574, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561983, 'N', 3405, 34224751, 571, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561984, 'N', 3405, 34224751, 570, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561985, 'N', 3405, 34224751, 568, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561986, 'N', 3405, 34224751, 567, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561987, 'N', 3405, 34224751, 566, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561989, 'N', 3405, 34224751, 563, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561990, 'N', 3405, 34224751, 562, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561991, 'N', 3405, 34224751, 560, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561992, 'N', 3405, 34224751, 559, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561993, 'N', 3405, 34224751, 558, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561994, 'N', 3405, 34224751, 548, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80561995, 'N', 3405, 34224751, 546, 35417360);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562160, 'N', 3405, 37382613, 139, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562161, 'N', 3405, 37382613, 140, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562162, 'N', 3405, 37382613, 141, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562163, 'N', 3405, 37382613, 142, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562164, 'N', 3405, 37382613, 143, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562165, 'N', 3405, 37382613, 145, 35291878);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562166, 'N', 3405, 37382613, 100, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562167, 'N', 3405, 37382613, 102, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562171, 'N', 3405, 37382613, 107, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562172, 'N', 3405, 37382613, 108, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562174, 'N', 3405, 37382613, 110, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562175, 'N', 3405, 37382613, 111, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562176, 'N', 3405, 37382613, 112, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562177, 'N', 3405, 37382613, 113, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562182, 'N', 3405, 37382613, 123, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562183, 'N', 3405, 37382613, 121, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562184, 'N', 3405, 37382613, 120, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562185, 'N', 3405, 37382613, 118, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562186, 'N', 3405, 37382613, 117, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562187, 'N', 3405, 37382613, 116, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562189, 'N', 3405, 37382613, 95, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562190, 'N', 3405, 37382613, 94, 35291883);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562213, 'N', 3405, 37382613, 89, 35291872);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (80562240, 'N', 3405, 35417256, 516, 35417271);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329559, 'N', 3405, 35329152, 443, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329560, 'N', 3405, 35329152, 444, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329562, 'N', 3405, 35329152, 446, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329109, 'N', 3405, 35329152, 433, 35329181);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329169, 'N', 3405, 35329152, 434, 35329181);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329557, 'N', 3405, 35329152, 441, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329558, 'N', 3405, 35329152, 442, 35329551);
    Insert into BUILDINGSV
       (BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
    Values
       (35329191, 'N', 3405, 35329152, 436, 35329181);
    COMMIT;
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (0, 'OSIUNKNOWN');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (9002284, 'THE GROVE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (9002364, 'DUBLIN ROAD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (9002375, 'NEWTOWN ROAD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35291872, 'HAZELHATCH ROAD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35291878, 'SIMMONSTOWN PARK');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35291883, 'PRIMROSE HILL');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329181, 'THE COPSE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329213, 'THE COURT');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329529, 'THE CRESCENT');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329551, 'THE LAWNS');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35329580, 'THE DRIVE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35417271, 'TEMPLEMILLS COTTAGES');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (35417360, 'CHELMSFORD');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (36500023, 'THE CLOSE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (36500101, 'THE GREEN');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375569, 'THE DOWNS');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375595, 'THE PARK');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375754, 'THE AVENUE');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37375781, 'THE VIEW');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37376046, 'THE CRESCENT');
    Insert into THORV
       (THORFARE_ID, THORFARE_NAME)
    Values
       (37376048, 'THE GLADE');
    COMMIT;
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (34224751, 'SIMMONSTOWN');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (35417256, 'TEMPLEMILLS');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (35329152, 'TEMPLE MANOR');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (37382613, 'CELBRIDGE');
    Insert into LOCV
       (LOCALITY_ID, NAME)
    Values
       (37375570, 'SAINT WOLSTAN''S ABBEY');
    COMMIT;
    ------------------------------------------------------------------------------Now the query with wrong result:
    select decode(th.thorfare_name,'OSIUNKNOWN',NULL,th.thorfare_name)
               THOR,
                l.name LOC,
                b.sequence_no SEQ,
               CASE WHEN th.thorfare_name = LAG (th.thorfare_name)
                OVER (order by b.sequence_no)
                or th.thorfare_name = LEAD (th.thorfare_name)
                OVER (order by b.sequence_no)
                THEN  COUNT(b.sequence_no) OVER (partition by th.thorfare_name,l.name order BY b.sequence_no
              ELSE 1
              END COUNT   from BUILDINGSV b,THORV th,LOCV l                 
    where   th.thorfare_id = b.thorfare_id
    and    nvl(b.invalid,'N')='N'
    and    b.route_id=3405
    and    b.locality_id =l.locality_id(+)
    order by b.sequence_no;The query result -WRONG (only first few lines)
    THOR                        LOC        SEQ    COUNT
    DUBLIN ROAD     CELBRIDGE    1     1
    NEWTOWN ROAD     CELBRIDGE        2     1
    NEWTOWN ROAD     CELBRIDGE        5     2
    THE GROVE     CELBRIDGE        6     1
    NEWTOWN ROAD     CELBRIDGE        7     3
    NEWTOWN ROAD     CELBRIDGE        9     4
    NEWTOWN ROAD     CELBRIDGE       10     5
    NEWTOWN ROAD     CELBRIDGE       11     6
    NEWTOWN ROAD     CELBRIDGE       12     7
    DUBLIN ROAD     CELBRIDGE       15     1
    PRIMROSE HILL     CELBRIDGE       43     1
    PRIMROSE HILL     CELBRIDGE       44     2
    DUBLIN ROAD     CELBRIDGE       52     3
    DUBLIN ROAD     CELBRIDGE       55     4
    DUBLIN ROAD     CELBRIDGE       56     5
    DUBLIN ROAD     CELBRIDGE       57     6
    DUBLIN ROAD     CELBRIDGE       58     7
    PRIMROSE HILL     CELBRIDGE       61     3
    PRIMROSE HILL     CELBRIDGE       62     4
    HAZELHATCH ROAD     CELBRIDGE       64     1
    HAZELHATCH ROAD     CELBRIDGE       65     2The query result -EXPECTED (only first few lines)
    THOR                     LOC     COUNT
    DUBLIN ROAD     CELBRIDGE      1
    NEWTOWN ROAD     CELBRIDGE      2
    THE GROVE     CELBRIDGE      1
    NEWTOWN ROAD     CELBRIDGE      5
    DUBLIN ROAD     CELBRIDGE      1
    PRIMROSE HILL     CELBRIDGE      2
    DUBLIN ROAD     CELBRIDGE      5
    PRIMROSE HILL     CELBRIDGE      2
    HAZELHATCH ROAD     CELBRIDGE      2Please note, in the expected result, I only need 1 row but need to show the total count of rows until the names change.
    So the issues are
    1) the count column values are wrong in my query.
    2)I dont want to repeat the same rows(Please see EXPECTED output and compare it against the original)
    3)Want the output in exactly same way as in EXPECTED OUTPUT as I dont want to group by thor name(Eg. I dont want the count for all DUBLIN ROAD but I want to examine rows for the next one, if THOR/LOC combination is different in next row then COUNT=1 else COUNT=Count of no. of rows for that thor/loc combination until the combination change -So there are same value multiple rows which i need to show it in 1 row with the total count)
    I am explaining below this in more detail!!
    I only need 1 row per same THOR/LOC names coming multiple times but I need the count shown against that 1 row(i.e COUNT= how many rows with same thor/loc combination until THOR/LOC combo changes value).
    Then repeat the process until all rows are finished..
    If there is no multiple row with same THOR/LOC coming in the following row-i.e the following row is a different THOR/LOC combination, then the count for that row is 1.
    Hope this is clear.
    Is this doable?
    Thanks in advance.
    Edited by: Krithi on 04-Nov-2010 07:45
    Edited by: Krithi on 04-Nov-2010 07:45
    Edited by: Krithi on 04-Nov-2010 08:31

  • Need analytic function suggestion

    Hi,
    I need advice related to analytic ( I think ) function in Oracle 9.
    create table testx ( id number, arr number, fore number, actual number, result_x number, is_first number);
    insert into testx values ( 1, null, null, 12, null , 0 );
    insert into testx values ( 2, null, null, 14 , null, 0 );
    insert into testx values ( 3, 4, 5, 16, 16, 1 );
    insert into testx values ( 4, 5, 5, 18, 16, 0 );
    insert into testx values ( 5, 5, 5, 20, 16, 0 );
    insert into testx values ( 6, 5, 5, 22, 16, 0 );
    insert into testx values ( 7, 5, 5, 24, 16, 0 );
    insert into testx values ( 8, 5, 5, 25, 16, 0 );
    insert into testx values ( 9, 5, 8, 25, 13, 0 );
    insert into testx values ( 10, 5, 8, 21, 10, 0 );
    insert into testx values ( 11, 5, 8, 19, 7, 0 );
    insert into testx values ( 12, 5, 8, 18, 4, 0 );
    I need ONE level query ( no subqueries ) which will calculate value stored in RESULT_X column.
    Rule for calculation is:
    1. when arr and fore columns are available first time then result_x = actual ( row with id = 3)
    2. in other case result_x = (previous value of result_x + arr - fore )
    3. order of records is stored in id column
    I have problem with calculating previous value of result_x since it should be available in next row calculation and dependents on other columns values.
    Thanks for help,
    Regards,
    Piotr

    Hi, Piotr,
    This produces the results you requested:
    SELECT       testx.*
    ,       SUM ( CASE
                  WHEN  is_first = 1
                  THEN  result_x
                  ELSE  arr - fore
                 END
               ) OVER (ORDER BY  id)     AS computed_result_x
    FROM      testx
    ORDER BY  id
    ;This relies on the fact that there is only one row where is_first=1, and that all the earlier rows have NULL as arr or fore.
    If that's not the case in your real data, then I don't think it's possible in SQL without sub-queries. Why can't you use a sub-query?
    The problem is that rows up to the one with is_first=1 have to be treated differently from rows after that point, so ithe CASE expression might need to know if a given row is before or after the one with is_first=1. If you need an analytic function to determine that, then you need a sub-query, becuase analytic functions can not be nested.
    You could use MODEL or a recursive WITH clause to get the results you want, but they require sub-querries.

  • IOS6 Headset / Headphone "Skip to the next chapter" & "Return to the previous chapter" functions don't work in the Music App

    "Skip to the next chapter" & "Return to the previous chapter" functions don't work in the iOS 6 Music App when listening to Podcasts and Audiobooks through Headsets (Apple & 3rd party), Speaker Docks, and in my autos handsfree bluetooth system (Ford Sync).
    Listening with the Podcasts App allows "Skip" and "Return" to work. So, this is either a bug or Apple is forcing the use of the Podcast App to get the "Skip" & "Return" controls. All the other headset functions work as designed.
    I listen to news Podcasts in chronological order. In iTunes I set up smart playlists sorted by release date for the podcasts. The Podcast App doesn't support playlists therefore I need to use the Music App.
    Is anyone able to get "Skip" and "Return" to work in the Music app?
    Thanks for any insight!

    We are fellow users here on these forums, you're not talking to iTunes Support nor Apple. If you want to leave feedback for Apple then you can do so here : http://www.apple.com/feedback/ipad.html

  • I cannot get my e-mail to automatically enter addresses in the "TO:" box. In the past, I would get a list of e-mail recipients to appear when I typed 1 letter. That function no longer works. Do I need to change a setting or what?

    I cannot get my e-mail to automatically enter addresses in the "TO:" box. In the past, I would get a list of e-mail recipients to appear when I typed 1 letter. That function no longer works. Do I need to change a setting or what?

    Settings > Store > Sign Out.
    Sign in with the correct ID.

Maybe you are looking for

  • Custom domain name pointing issues

    Hi I've set my custom domain name for my webpages and it is working fine - www.domain.com points to my iweb site. But how can i make sure that domain.com (without the www.) also points to my iweb site? I know I could change settings on my domain regi

  • IPhoto 6.0 and IMovie6.0 no audio and audio effects

    IPhoto I can't hear any audio in the slide show (Music from ITunes or Demo Songs Garage Band) for exampels or when i add the Music to a slide Show. IMovie In IMovie i can't hear after the import of a film from JVC over Fire Wire the audio and then is

  • Crop pdf file

    I received pdf file as attachment in Mail. I copied the pdf file to iBook. How do you crop the pdf? I tried iBook, goodreader, easypdf, documents 2 to open the pdf however there is no feature to crop.

  • Multiple chat clients with Webcam support

    Hi; I switched to the mac and i was wondering if anyone knows a program that will let me connect to Yahoo, MSN, and AOL at the same time and use multiple accounts. I also want webcam support. Currently i use different programs and i would like to be

  • [CS3] Unable to open CS4 created .inx file

    I ran into an error when attempting to open an InDeseign CS4 generated .inx file which was created off-site in CS3. The error nortification I received stated that it was due to an "unterminated CDATA section." CS3 is curently at 5.0.4 on a Mac platfo