Partition of cumulative sum of length

Hello

I'm working on a code to work around the limitation of wm_concat.  Please consider the following data:

string_data

---------

AAAAA

BBB

CCC

DELAHAYE

EA

FFFFFF

GG

HARMAN

To this day, I have the following statement:

Sum (length (string_data)) (string_data order)

My current results are:

AAAAA 5

BBB 8

CCC 11

DDDDDDD 18

I want to do is be able to break once reached a certain length, say for example 12.  So, I want to wm_concat on this particular window or a set of results.  Example:

YYYY, BBB, CCC

DELAHAYE, EE

FFFFFF, GG

Is someone can please help me overcome this barrier?

Thank you very much.

Try this:

with the data as

(

Select "AAAAA" str in union double all the

Select "BBB-" of all the double union

Select "CCC" in all the double union

Select 'VAL' Union double all the

Select 'EE' of all the double union

Select 'FFFFFF' Union double all the

Select "GG" of all the double union

Select "HARMAN" double

)

SELECT MAX (new_str)

DE)

SELECT new_str, grp

FROM (SELECT * FROM data ORDER BY str)

DIMENSION of the MODEL BY (rn ROWNUM) measures (CAST (str AS VARCHAR2 (4000)) new_str, str, grp 0)

RULES)

new_str [any] order by rn = CASE WHEN CV (rn) > 1 AND LENGTH (new_str [cv () - 1] |) «, » || Str [CV ()])<=12 then="">

Of ANOTHER str [cv ()]

END

GRP [any] = nvl (grp [cv () - 1], 0) + BOX WHEN new_str [cv ()] = str [cv ()] then 1 else 0 end

)

)

GROUP BY grp;

Tags: Database

Similar Questions

  • Cumulative sum of OBIEE 11 g

    Hello
    I have generated a report, need to create the cumulative sum in my reports.can someone give me the solution how do.cummulative are like
    col1 col2 col 3 (cummulative_sum)
    A 5 5
    B-10-15 (5 + 10)
    15-30 (15 + 15) c
    receive your answer.


    Brgds,
    GN

    Published by: GN on January 31, 2012 03:48

    Hello

    You can use ABSTRACT (Measure_Column).

    Concerning
    Young

    NOTE - Please make this thread closed / or still facing issue so that she could help others.

  • find the cumulative sum of the previous columns

    Hello

    How can I get the cumulative sum of a particular column in the preceding lines in the current line.
    e.g. I have a table produced,

    Date | Orders
    __________________
    1 JANUARY 10 | 3
    JANUARY 2, 10. 5
    JANUARY 3, 10. 1

    I want to select the date and orders between the 1st and the 3rd. Something like...
    Select the Date, the orders for products where Date between to_date('1-JAN-10') and to_date('3-JAN-10')

    But I want the output like this...

    Date | Orders
    __________________
    1 JANUARY 10 | 0
    JANUARY 2, 10. 3
    JANUARY 3, 10. 8

    Similarly, when I select it between 2 and 3, I want the output voltage...

    Date | Orders
    _________________
    JANUARY 2, 10. 3
    JANUARY 3, 10. 8

    and so on. In the output, orders here are the cumulative sum of the previous columns.

    Published by: eric clapton, November 8, 2010 20:45

    Hello

    Use the analytical SUM function.
    You want to filter by date after the cumulative SUM is calculated, so calculate the SUM in a subquery, then filter by date no later than:

    WITH     got_cm_orders     AS
    (
         SELECT     dt
         ,     orders
         ,     SUM (orders) OVER (ORDER BY dt) - orders
                   AS cm_orders
         FROM     products
    )
    SELECT     dt
    ,     orders
    WHERE     dt     BETWEEN     DATE '2010-01-02'
              AND     DATE '2010-01-03'
    ;
    
  • Cumulative sum of 3.1 to 6 1 and 1 to 9 and 1 to 11 lines

    How to get the cumulative sum of follow-up records since of 1To 3.1 to 6 and 1 to 9 and 1 to 11

    A sum (AVG) AVERAGE of BEG END TEACHER
    --------------------------------------------------
    1 0 0.1 159 159
    2 0.1 0.2 159 168
    3 0.2 0.3 179 159 486 (sum of lines 1 to 3)
    4 0.1 0.2 250 300
    5 0.2 0.3 320 250
    6 0.4 0.3 250 380 1416 (sum of lines 1 to 6)
    7 0.2 0.3 388 379
    8 0.3 0.4 379 388
    9 388 400 2583 0.5 0.4 (sum of lines 1 through 9)
    10 1.5 0.6 499 500
    11 420 448 3531 0.5 0.6 (sum of lines 1 to 11)


    create table TABLEAA
    (
    A NUMBER,
    PLEASE THE NUMBER,
    NUMBER OF END,
    NUMBER OF TEACHER,
    AVERAGE NUMBER
    )
    ;

    Fast loading TABLEAA...
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (10, 1.5,.6,, 499, 500);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (1, 0,.1, 159, 159);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (2,.1,.2,, 159, 168);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (3,.2,.3, 179, 159);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (4,.1,.2, 250, 300);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (5,.2,.3, 320, 250);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (6,.3,.4,, 250, 380);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (7,.2,.3, 388, 379);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (8,.3,.4,, 379, 388);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (9,.4,.5, 388, 400);
    insert into TABLEAA (A, START, END, TEACHER, AVERAGE)
    values (11.5.6, 420, 448);
    commit;
    SQL>  select t.*, case
                               when mod (
                                             row_number () over (order by a),
                                             3
                                        ) = 0
                                   or a = last_value (a) over ()
                               then
                                    sum (average) over (order by a)
                          end
                               s
          from tableaa t
    order by a
    /
             A        BEG        END       PROF    AVERAGE          S
    ---------- ---------- ---------- ---------- ---------- ----------
             1          0         ,1        159        159
             2         ,1         ,2        159        168
             3         ,2         ,3        179        159        486
             4         ,1         ,2        250        300
             5         ,2         ,3        320        250
             6         ,3         ,4        250        380       1416
             7         ,2         ,3        388        379
             8         ,3         ,4        379        388
             9         ,4         ,5        388        400       2583
            10        1,5         ,6        499        500
            11         ,5         ,6        420        448       3531
    
    11 rows selected.
    
  • get the cumulative sum of the salaries of the employees of emp table

    Hi gurus,

    Can any body tell me how to get the cumulative sum of the salaries of the employees in the emp table using analytical functions.

    Thanks in advance...

    Please read the document

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/functions163.htm#sthref2186

    Look for example at the bottom of the document.

  • Cumulative sum

    How to calculate a running sum, A = A + B, in Labview?  I've seen some exaples of how to do it with process variables, a while loop and registers at offset.  A loop time won't work in my application because it calculates the sum of each waiting period.   Where I want to sum only when new orders X and are sent to a stepper motor controller.

    The sums of X and Y will be used to show how an X and table Y went from a "Home" position.

    Howard

    Either a business structure or a selection function will work.  The structure of the case is perhaps easier.  Develop the structure of the case around the addition, with add it inside of the case of fake.  In the case of true, connect the value that you add directly to the exit tunnel (not more).  This way you can reset any value you want.  The value to add will be outside the structure of the case, of course, and for maximum efficiency, it is also suitable for outside the while loop.  If this is not clear, or you want someone to check your work, attach your VI.  Make sure that you reset the first time you run the VI (or handle this within the race in summary VI by using the "First Call" function and OR the output with the reset entry).

  • Get the cumulative sum

    Hello

    What can I do to obatain the net sum of these records

    day                          net
    ------------------------------------------------
    01-03-12 00:00:00     41946,4
    02-03-12 00:00:00     45263,82
    03-03-12 00:00:00     64980,98
    04-03-12 00:00:00     42695,22
    05-03-12 00:00:00     29678,96
    06-03-12 00:00:00     30537,88
    07-03-12 00:00:00     31348,47
    08-03-12 00:00:00     50076,46
    09-03-12 00:00:00     45184,64
    10-03-12 00:00:00     53773,22
    Thanks in advance
  • Total cumulative peform Records

    My requirement is to make the accumulated amount while they inspected the highest bonus.

    Here are the files in the sample.

    create the table test_cumsum as

    (select 100 'id', 5 'bonus', 0.25 "CLTS', 'y' flg in union double all the)

    Select 3, 200, 0.25, 'y' from dual union all

    Select 300,1,0.15, 'y' from dual union all

    Select 400,2,0.25, 'y' from dual union all

    Select 500,6,0.25, 'y' in the double)

    From the award-winning sum the CLTS hightest until the cumulative sum = 1

    Records that are left in the running sum must be updated with flg = N

    In the example above the SumOf CLTS for 6,5,3,2 bonus will be equal to 1

    So record id = 300 must be updated to flg = N as long as this record needs no summation of the value to 1

    Please suggest!

    Could not test this carefully... but something like this may work. Test it with improved sample just to audit data.

    ----------

    SELECT "id", "bonus", "crdts",
           CASE
              WHEN SUM (
                      "crdts")
                   OVER (PARTITION BY 1
                         ORDER BY "bonus" DESC
                         ROWS UNBOUNDED PRECEDING) <= 1
              THEN
                 'Y'
              ELSE
                 'N'
           END
              flg
      FROM test_cumsum;
    
    ID bonus CLTS FLG
    500 6 0.25 THERE
    100 5 0.25 THERE
    200 3 0.25 THERE
    400 2 0.25 THERE
    300 1 0.15 N

    p.s. Please name the columns without quotation marks double... its just pain

    See you soon,.

    Manik.

  • running sum

    Hello world

    I have to make a running sum and I don't know how to in SQL.

    This is my table:

    AxisLine_orderLinePK
    A1105
    A1107
    A2209
    A33010
    A33015
    A33012
    B14013
    B14018
    B25014
    B25015
    B2505

    I have to do a cumulative sum of PK in the following order line (line_order) for each axis.

    I have to me like this:


    AxisLine_orderLinePKRunsum_pk
    A11055
    A110712
    A220921
    A3301031
    A3301546
    A3301258
    B1401313
    B1401831
    B2501445
    B2501560
    B250565



    any suggestion?

    Thank you for your help.



    Hello

    WITH tab1 AS (SELECT "A" axis, 1 Line_order, 10 Line, 5 Double PK

    UNION ALL

    SELECT "A" 1, 10, 7 DOUBLE

    UNION ALL

    SELECT "A" 2, 20, 9 DOUBLE

    UNION ALL

    SELECT "A" 3, 30, 10 DOUBLE

    UNION ALL

    SELECT "A" 3, 30, 15 DOUBLE

    UNION ALL

    SELECT "A" 3, 30, 12 DOUBLE

    UNION ALL

    SELECT 'B ',. 1, 40, 13 DOUBLE

    UNION ALL

    SELECT 'B ',. 1, 40, 18 DOUBLE

    UNION ALL

    SELECT 'B ',. 2, 50, 14 DOUBLE

    UNION ALL

    SELECT 'B ',. 2, 50, 15 DOUBLE

    UNION ALL

    SELECT 'B ',. 2, 50, 5 double),

    tab2 AS (SELECT ROWNUM rn, axis, Line_order, line, pk FROM tab1)

    SELECT d.rn, d.Axis, d.Line_order, d.Line, d.pk, d.pk + nvl ((SELECT sum (b.pk) FROM tab2 b WHERE b.Axis = d.Axis and b.rn< d.rn),0)="">

    of the tab2 d

    Kind regards

    Fame

  • Filtered sum

    I created the following query:

    SELECT DISTINCT
    user_id,
    order_item_id,
    (SELECT
    COUNT (*)
    Of
    buns
    WHERE
    order_item_id = r.order_item_id AND
    user_id = r.user_id
    ) num,.
    (SELECT
    SUM (film)
    Of
    buns
    WHERE
    order_item_id = r.order_item_id AND
    user_id = r.user_id AND
    grade_id = 4
    ).
    (SELECT
    SUM (film)
    Of
    buns
    WHERE
    order_item_id = r.order_item_id AND
    user_id = r.user_id AND
    grade_id! = 4
    ) bad
    Of
    r rolls
    WHERE
    To_char (r.date_of_rewind, ' JJ.) (MM. RRRR') = ' 02.12.2010.

    I get good results, but the query performs a lot better when I use COUNT (*) OVER (PARTITION BY user_id, order_item_id) for the name.
    Is it possible to PARTITION BY the sums with the additional conditions for good and bad?

    Pawel
    SUM(CASE WHEN grade = 4 THEN metrage ELSE 0 END) good,
    SUM(CASE WHEN grade != 4 THEN metrage ELSE 0 END) bad
    
  • cumsum

    Hello

    I want to generate a vector that is the cumulative sum of another vector without using a loop. My vector is more than 10 ^ 6 length and a loop takes forever. I use cumsum in matlab, but could not find something similar in labview.

    Thank you!

    It works for a 1 d of 1 million items table... you need to mod a little for a 2d array but it takes hardly any time at all to run. You must have an inefficiency in your code. If you make columns or lines, I can mod a bit for you, but you should try it on your own first using this example.

  • Valid time intervals: two tables intersect documents

    Benefits:

    I have two tables:

    AVAILABILITY (ALLOCATION A_ID, START_TIME, END_TIME, ID_RESSOURCE)

    TASK (T_ID, START_TIME, END_TIME, ID_RESSOURCE)

    My final request should print:

    Id_ressource, DAY_OF_WEEK, MYDATE, DURATION (in hours)

    Semantics back: "give me all the time slots available; Time slot available to the general public is the time range that does not contain a task"

    1. in the tables above START_TIME, END_TIME are of type DATE and they contain a timestamp that is to say:

    SELECT to_char (START_TIME, 'HH24 MMDDYYYY') from the AVAILABILITY/TASK

    would print something like

    09232014 14:17

    2. in the tables above time START_TIME and END_TIME are of the same date and END_TIME > START_TIME IE

    START_TIME = 09232014 14:17

    END_TIME = 09232014 15:17

    no need to check that

    3. If for some date there are TASK BOTH records and AVAILABILITY, then it must be:

    TASK. End_time < = AVAILABILITY. End_time and TASK. Start_time = > AVAILABILITY. START_TIME

    In other words, JOB registration is still "inside" free and BUSY folder

    I need time slots which are essentially output

    -All available records that do not contain a ' ' tasks.

    AVAILABILITY. Id_ressource, to_char (AVAILABILITY. Start_time, 'DAY') as DAY_OF_WEEK, AVAILABILITY. Start_time as MYDATE, (AVAILABILITY. END_TIME-AVAILABILITY. Start_time) * 24 as DURATION

    AND

    -If the free and busy folder contains task (see 3) then there are 2 two time intervals resulting (Yes the duration can be 0)

    AVAILABILITY. Id_ressource, to_char (AVAILABILITY. Start_time, 'DAY') as DAY_OF_WEEK, AVAILABILITY. Start_time as MYDATE, (TASK. START_TIME-AVAILABILITY. Start_time) * 24 as DURATION

    and

    AVAILABILITY. Id_ressource, to_char (AVAILABILITY. Start_time, 'DAY') as DAY_OF_WEEK, AVAILABILITY. Start_time as MYDATE, (AVAILABILITY. END_TIME-TASK. End_time) * 24 as DURATION

    Hope the above makes sense. What is the best, more rapid SQL?

    Hello

    Here's a way to do it:

    WITH union_data AS

    (

    SELECT id_ressource, change_time, change_val

    AVAILABILITY

    UNPIVOT (change_time

    FOR change_val IN (start_time AS 1

    end_time AS - 1

    )

    )

    UNION ALL

    SELECT id_ressource, change_time, change_val

    TASK

    UNPIVOT (change_time

    FOR change_val IN (start_time AS 1

    end_time AS - 1

    )

    )

    )

    got_change_sum AS

    (

    SELECT id_ressource

    change_time

    SUM (change_val) OVER (PARTITION BY id_ressource

    ORDER BY change_time

    ) AS change_sum

    Advance (change_time) OVER (PARTITION BY id_ressource

    ORDER BY change_time

    ) - change_time AS long

    Of union_data

    )

    SELECT id_ressource

    , TO_CHAR (change_time, 'fmDay, FMMM-DD-YYYY') AS available_date

    Duration * 24 AS available_hours

    OF got_change_sum

    WHERE change_sum = 1

    ORDER BY id_ressource

    change_time

    ;

    To see how it works, view the results of each auxiliary request individually.

    You will see that the union_data product has all the points where either avaialablity or a task changes.  The column change_val is + 1 availability or a task at the beginning and - 1, when it ends.  When the cumulative sum of those is 0, this means that there is no availability.  When the accumulated amount is > 1, this means a task is underway.  We are interested in the places where the cumulative sum of change_val is exactly 1, sense that a period which started (but not yet completed) and that there is a net 0 current tasks.

    This solution is NOT the value of start_time and end_time in the same day of calendar and tasks for the same resource may overlap.

  • Insert table when missing data

    Hello
    I have the following table, for readability I cracked the after each manager for each file number. The last column is the cumulative sum of the second last column. Value for month should ideally go 1 - 3 (as we see in both cases to Chicago). But the table, you can see in some cases, some entries are missing (marked by <-).

    CITY CASE_NUMBER CASE MANAGER MONTHS MONTHLY_TOTAL FISCAL_TOTAL
    ---------------------------------------------------------------------------
    Chicago case_1 1 John 1 2 2
    Chicago case_1 1 John 2 3 5
    Chicago case_1 1 John 3 5 10

    Chicago case_1 Jeff 1 4 4 1
    Chicago case_1 Jeff 2 2 6 1
    Chicago case_1 Jeff 3 3 9 1

    Chicago case_2 John 2 1 3 3
    Chicago case_2 John 2 2 2 5
    Chicago case_2 John 2 3 4 9

    Chicago case_2 Jeff 2 1 2 2
    Chicago Jeff 2 2 7 9 case_2 < -.

    NewYork case_1 1 Lee 1 3 3
    NewYork case_1 Lee 2 4 7 1 < -.

    case_1 NewYork 1 Sue 1 2 2
    case_1 NewYork 1 Sue 2 3 5
    case_1 NewYork 1 Sue 3 2 7

    NewYork case_1 Lee 2 1 2 2
    NewYork case_1 Lee 2 2 4 6
    NewYork case_1 Lee 2 3 4 10

    NewYork case_1 Sue 1 3 3 2
    NewYork case_1 2 Sue 2 2 5 < -.


    What I want is first find the missing lines and insert values. To find those missing monthly_total = 0
    fiscal_total = value of the previous row. For example, for the missing first line should be:

    CITY CASE_NUMBER CASE MANAGER MONTHS MONTHLY_TOTAL FISCAL_TOTAL
    ---------------------------------------------------------------------------
    Chicago case_2 2 3 0 9 Jeff
    with t1 as (
                select  case_number,
                        max(month) month_count
                  from  tbl
                  group by case_number
               ),
         t2 as (
                select  case_number,
                        column_value month
                  from  t1,
                        table(
                              cast(
                                   multiset(
                                            select  level
                                              from  dual
                                              connect by level <= month_count
                                           )
                                   as sys.OdciNumberList
                                  )
                             )
               )
    select  max(city) over(partition by t2.case_number,manager) city,
            max(case) over(partition by t2.case_number) case,
            t2.case_number,
            t.manager manager,
            t2.month month,
            nvl(monthly_total,0) monthly_total,
            last_value(fiscal_total ignore nulls) over(partition by t2.case_number,manager order by t2.month) fiscal_total
      from      tbl t
              partition by(manager)
            right join
                t2
              on (
                      t2.case_number = t.case_number
                  and
                      t2.month = t.month
                 )
      order by t2.case_number,
               manager,
               month
    /
    
    CITY    CASE   CASE_NUMBER MANA      MONTH MONTHLY_TOTAL FISCAL_TOTAL
    ------- ------ ----------- ---- ---------- ------------- ------------
    chicago case_1           1 Jeff          1             4            4
    chicago case_1           1 Jeff          2             2            6
    chicago case_1           1 Jeff          3             3            9
    chicago case_1           1 John          1             2            2
    chicago case_1           1 John          2             3            5
    chicago case_1           1 John          3             5           10
    newyork case_1           1 Lee           1             3            3
    newyork case_1           1 Lee           2             4            7
    newyork case_1           1 Lee           3             0            7 <-- missing row
    newyork case_1           1 Sue           1             2            2
    newyork case_1           1 Sue           2             3            5
    
    CITY    CASE   CASE_NUMBER MANA      MONTH MONTHLY_TOTAL FISCAL_TOTAL
    ------- ------ ----------- ---- ---------- ------------- ------------
    newyork case_1           1 Sue           3             2            7
    chicago case_2           2 Jeff          1             2            2
    chicago case_2           2 Jeff          2             7            9
    chicago case_2           2 Jeff          3             0            9 <-- missing row
    chicago case_2           2 John          1             3            3
    chicago case_2           2 John          2             2            5
    chicago case_2           2 John          3             4            9
    newyork case_2           2 Lee           1             2            2
    newyork case_2           2 Lee           2             4            6
    newyork case_2           2 Lee           3             4           10
    newyork case_2           2 Sue           1             3            3
    
    CITY    CASE   CASE_NUMBER MANA      MONTH MONTHLY_TOTAL FISCAL_TOTAL
    ------- ------ ----------- ---- ---------- ------------- ------------
    newyork case_2           2 Sue           2             2            5
    newyork case_2           2 Sue           3             0            5 <-- missing row
    
    24 rows selected.
    
    SQL> 
    

    SY.

  • Group by month

    Hi, I have a query that calculates the length total telephone lines which are put into service (in service) and not private for a period (V_DATE_FROM & V_DATE_TO) data as shown below:

    SELECT SUM (NVL (c.length, 0))
    OF tel_lines c
    WHERE (c.code, c.date_commissioned)
    IN
    (SELECT c1.code, MAX (c1.date_commissioned)
    Tel_lines C1
    WHERE the c1.date_commissioned <: V_DATE_TO
    AND (c1.date_decommissioned >: V_DATE_FROM)
    GOLD date_decommissioned IS NULL)
    AND (c1.date_privately_owned >: V_DATE_FROM)
    GOLD c1.date_privately_owned IS NULL)
    GROUP BY c1.code)

    For example, my V_DATE_FROM is 01/01/2011 and my V_DATE_TO is 30/06/2011. As a result, I got the total length of the lines put into service as of 30/06/2011. If a line has been downgraded Let's say 01/03/2011, the length of this line will not appear in the calculation of the length total for the period January-June 2011 but is included for the period January-February 2011. Thus, it is possible that the January-February period will have a higher value in the period from January to June.

    Now, what I want to do is to get the MONTHLY TOTAL LENGTH HIGHER for a given period, such as if the total for the period January-June 2011 is on February, then what I would get either the value of February for the period Jan-June 2011.

    I hope that my explanation is understood. Thanks in advance for the help.

    Hello

    I don't know, why the column code is missing in your example, or if it was online now?

    with v_date_to = date ' 2011-06-30' and v_date_from = date ' 2011-01-01'

    DROP TABLE tel_lines;
    CREATE TABLE tel_lines (
         code  VARCHAR2(1)
        ,lines VARCHAR2(1)
        ,length NUMBER
        ,date_commissioned DATE
        ,date_decommissioned DATE
        );
    
    INSERT INTO tel_lines(code,lines,length,date_commissioned,date_decommissioned)
    VALUES('K','A',10,DATE '2000-01-01',NULL);
    INSERT INTO tel_lines(code,lines,length,date_commissioned,date_decommissioned)
    VALUES('K','B',15,DATE '2000-01-02',NULL);
    INSERT INTO tel_lines(code,lines,length,date_commissioned,date_decommissioned)
    VALUES('K','C',20,DATE '2000-01-03',NULL);
    INSERT INTO tel_lines(code,lines,length,date_commissioned,date_decommissioned)
    VALUES('K','D',25,DATE '2000-01-04',DATE '2011-05-31');
    INSERT INTO tel_lines(code,lines,length,date_commissioned,date_decommissioned)
    VALUES('K','E',30,DATE '2011-06-01',NULL);
    COMMIT;
    
    WITH months AS (
        SELECT  ADD_MONTHS(:V_DATE_FROM,LEVEL-1) m_first
               ,LAST_DAY(ADD_MONTHS(:V_DATE_FROM,LEVEL-1)) m_last
        FROM    dual
        CONNECT BY LEVEL < MONTHS_BETWEEN(:V_DATE_TO,:V_DATE_FROM) + 1
        )
    ,lines_per_month AS(
        SELECT  line_length
               ,MAX(line_length) OVER(PARTITION BY code) max_line_length
               ,m_first
               ,code
        FROM    (
                        SELECT  c1.code
                               ,SUM(c1.length) line_length
                               ,m_first
                        FROM     tel_lines c1 CROSS JOIN months
                        WHERE   c1.date_commissioned < m_last
                        AND     (  c1.date_decommissioned > m_first
                                OR date_decommissioned IS NULL)
                        GROUP BY c1.code
                                ,m_first
            )
        )
    SELECT  line_length
           ,m_first
           ,code
    FROM    lines_per_month
    WHERE   line_length = max_line_length;
    
    LINE_LENGTH            M_FIRST                   CODE
    ---------------------- ------------------------- ----
    75                     01.06.2011                K
    

    Concerning
    Marcus

  • Join query with union

    Hi all

    I have two requests and I want to join this two query

    The column of the report should be like this
    item_number WK_30  WE_31
    1st request
    select 
    re.item_number, 
    nvl(le.quantity,0) - nvl(re.quantity,0) WK_30
    from BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re
    where le.item_number =re.item_number
    and to_number(substr(re.year_week,-2,2)) = to_number(to_char(sysdate,'IW'))+1
    2nd request
    select 
    re.item_number, 
    nvl(le.quantity,0) - nvl(re.quantity,0) WK_31
    from BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re
    where le.item_number =re.item_number
    and to_number(substr(re.year_week,-2,2)) = to_number(to_char(sysdate,'IW'))+2
    Thanks in advance

    Concerning

    Hello

    You are welcome.

    I think that the best way to understand that is to show what data is generated at each stage...

    1. we need to generate a list of lines that have every week, we are interested in swing on...

    (   SELECT
            to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num
        FROM
            DUAL
        CONNECT BY LEVEL <= 5
    ) row_gen
    
    YEAR_WEEK_NUM
    -------------
               30
               31
               32
               33
               34
    

    2. must be added BACKLOG_WEEK_WH_AFTR_ATP lines in the calculations for the first week in the report

    SELECT
        le.item_number,
        le.quantity,
        to_number(to_char(sysdate,'IW'))+1 year_week_num
    FROM
        BACKLOG_WEEK_WH_AFTR_ATP le
    UNION ALL
    SELECT
        re.item_number,
        -re.quantity,
        to_number(substr(re.year_week,-2,2)) year_week_num
    FROM
        BACKLOG_ATP_GT_CW_IN re
    /
    ITEM_NUMBE   QUANTITY YEAR_WEEK_NUM
    ---------- ---------- -------------
    ITEM_1            200            30
    ITEM_2            600            30
    ITEM_1           -200            30
    ITEM_1           -100            33
    ITEM_2           -300            30
    ITEM_2           -200            32
    ITEM_3           -800            30
    ITEM_3           -400            34
    

    3. we must now repeat weekly, generated by the request of row_gen against all of the item_numbers returned by the above query and match them if possible to get the amount. Where there is no entry in BACKLOG_WEEK_WH_AFTR_ATP or BACKLOG_ATP_GT_CW_IN for a number of the week, we should use 0 as the quantity. The Partition Outer Join does for us.

    SQL>         select
      2              re.item_number,
      3              row_gen.year_week_num,
      4              NVL(re.quantity,0) qty
      5          from
      6              (   SELECT
      7                      to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num
      8                  FROM
      9                      DUAL
     10                  CONNECT BY LEVEL <= 5
     11              ) row_gen LEFT OUTER JOIN
     12                      (   SELECT
     13                              le.item_number,
     14                              le.quantity,
     15                              to_number(to_char(sysdate,'IW'))+1 year_week_num
     16                          FROM
     17                              BACKLOG_WEEK_WH_AFTR_ATP le
     18                          UNION ALL
     19                          SELECT
     20                              re.item_number,
     21                              -re.quantity,
     22                              to_number(substr(re.year_week,-2,2)) year_week_num
     23                          FROM
     24                              BACKLOG_ATP_GT_CW_IN re
     25                       ) re
     26                  PARTITION BY (re.item_number)
     27                  ON ( row_gen.year_week_num = re.year_week_num)
     28  /
    
    ITEM_NUMBE YEAR_WEEK_NUM        QTY
    ---------- ------------- ----------
    ITEM_1                30        200
    ITEM_1                30       -200
    ITEM_1                31          0
    ITEM_1                32          0
    ITEM_1                33       -100
    ITEM_1                34          0
    ITEM_2                30       -300
    ITEM_2                30        600
    ITEM_2                31          0
    ITEM_2                32       -200
    ITEM_2                33          0
    ITEM_2                34          0
    ITEM_3                30       -800
    ITEM_3                31          0
    ITEM_3                32          0
    ITEM_3                33          0
    ITEM_3                34       -400
    
    17 rows selected.
    

    4. now, we have the base dataset that we need - she has all the amounts with the right directions so that we can simply add everything up as a sum of sumulative. That's what makes OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) SUM (NVL(re.quantity,0)) bit. He said baseically are all lines seen so far in the result set for each item number...

            select
                re.item_number,
                row_gen.year_week_num,
                NVL(re.quantity,0) qty,
                SUM(NVL(re.quantity,0)) OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) quantity
            from
                (   SELECT
                        to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num
                    FROM
                        DUAL
                    CONNECT BY LEVEL <= 5
                ) row_gen LEFT OUTER JOIN
                        (   SELECT
                                le.item_number,
                                le.quantity,
                                to_number(to_char(sysdate,'IW'))+1 year_week_num
                            FROM
                                BACKLOG_WEEK_WH_AFTR_ATP le
                            UNION ALL
                            SELECT
                                re.item_number,
                                -re.quantity,
                                to_number(substr(re.year_week,-2,2)) year_week_num
                            FROM
                                BACKLOG_ATP_GT_CW_IN re
                         ) re
                    PARTITION BY (re.item_number)
                    ON ( row_gen.year_week_num = re.year_week_num)
    /
    ITEM_NUMBE YEAR_WEEK_NUM        QTY   QUANTITY
    ---------- ------------- ---------- ----------
    ITEM_1                30        200          0
    ITEM_1                30       -200          0
    ITEM_1                31          0          0
    ITEM_1                32          0          0
    ITEM_1                33       -100       -100
    ITEM_1                34          0       -100
    ITEM_2                30       -300        300
    ITEM_2                30        600        300
    ITEM_2                31          0        300
    ITEM_2                32       -200        100
    ITEM_2                33          0        100
    ITEM_2                34          0        100
    ITEM_3                30       -800       -800
    ITEM_3                31          0       -800
    ITEM_3                32          0       -800
    ITEM_3                33          0       -800
    ITEM_3                34       -400      -1200
    

    5. we can now rotate these data in columns - however, there is a small problem. For points 1 and 2 we have 2 lines for the week 30, running in the quantity column total is the same for both because the two lines appear at the same point in the game as defined by the order by clause. This means before rotate us, we must select one of the lines - or the other will do, they are both correct. If we do not have, the effect will be to double the cumulative sum (for point 1 is 0, so it does not appear) for this, we can use the ROW_NUMBER function...

            select
                re.item_number,
                row_gen.year_week_num,
                NVL(re.quantity,0) qty,
                SUM(NVL(re.quantity,0)) OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) quantity,
                ROW_NUMBER() OVER(PARTITION BY re.item_number, row_gen.year_week_num ORDER BY NULL) rn
            from
                (   SELECT
                        to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num
                    FROM
                        DUAL
                    CONNECT BY LEVEL <= 5
                ) row_gen LEFT OUTER JOIN
                        (   SELECT
                                le.item_number,
                                le.quantity,
                                to_number(to_char(sysdate,'IW'))+1 year_week_num
                            FROM
                                BACKLOG_WEEK_WH_AFTR_ATP le
                            UNION ALL
                            SELECT
                                re.item_number,
                                -re.quantity,
                                to_number(substr(re.year_week,-2,2)) year_week_num
                            FROM
                                BACKLOG_ATP_GT_CW_IN re
                         ) re
                    PARTITION BY (re.item_number)
                    ON ( row_gen.year_week_num = re.year_week_num)
    /
    ITEM_NUMBE YEAR_WEEK_NUM        QTY   QUANTITY         RN
    ---------- ------------- ---------- ---------- ----------
    ITEM_1                30        200          0          1
    ITEM_1                30       -200          0          2
    ITEM_1                31          0          0          1
    ITEM_1                32          0          0          1
    ITEM_1                33       -100       -100          1
    ITEM_1                34          0       -100          1
    ITEM_2                30       -300        300          1
    ITEM_2                30        600        300          2
    ITEM_2                31          0        300          1
    ITEM_2                32       -200        100          1
    ITEM_2                33          0        100          1
    ITEM_2                34          0        100          1
    ITEM_3                30       -800       -800          1
    ITEM_3                31          0       -800          1
    ITEM_3                32          0       -800          1
    ITEM_3                33          0       -800          1
    ITEM_3                34       -400      -1200          1
    

    6. so now that we have a way to select one of the duplicate lines, we can move forward a pivot data by ensuring that us choose only 'first' line for each element for each week...

    SELECT
        item_number,
        SUM
        (   CASE
                WHEN year_week_num = to_number(to_char(sysdate,'IW'))+1 THEN
                    quantity
            END
        ) plus_1,
        SUM
        (   CASE
                WHEN year_week_num = to_number(to_char(sysdate,'IW'))+2 THEN
                    quantity
            END
        ) plus_2,
        SUM
        (   CASE
                WHEN year_week_num = to_number(to_char(sysdate,'IW'))+3 THEN
                    quantity
            END
        ) plus_3,
        SUM
        (   CASE
                WHEN year_week_num = to_number(to_char(sysdate,'IW'))+4 THEN
                    quantity
            END
        ) plus_4,
        SUM
        (   CASE
                WHEN year_week_num = to_number(to_char(sysdate,'IW'))+5 THEN
                    quantity
            END
        ) plus_5
    FROM
        (   select
                re.item_number,
                row_gen.year_week_num,
                SUM(NVL(re.quantity,0)) OVER(PARTITION BY re.item_number ORDER BY row_gen.year_week_num) quantity,
                ROW_NUMBER() OVER(PARTITION BY re.item_number, row_gen.year_week_num ORDER BY NULL) rn
            from
                (   SELECT
                        to_number(to_char(sysdate,'IW')) + ROWNUM year_week_num
                    FROM
                        DUAL
                    CONNECT BY LEVEL <= 5
                ) row_gen LEFT OUTER JOIN
                        (   SELECT
                                le.item_number,
                                le.quantity,
                                to_number(to_char(sysdate,'IW'))+1 year_week_num
                            FROM
                                BACKLOG_WEEK_WH_AFTR_ATP le
                            UNION ALL
                            SELECT
                                re.item_number,
                                -re.quantity,
                                to_number(substr(re.year_week,-2,2)) year_week_num
                            FROM
                                BACKLOG_ATP_GT_CW_IN re
                         ) re
                    PARTITION BY (re.item_number)
                    ON ( row_gen.year_week_num = re.year_week_num)
        )
    WHERE
        rn = 1
    GROUP BY
        item_number
    

    You need not add a join between BACKLOG_WEEK_WH_AFTR_ATP and BACKLOG_ATP_GT_CW_IN, because we are all summed up in the article number and all item numbers, we have selected two tables. The aggregation ensures that the quantities are added together against the item number.

    HTH

    David

Maybe you are looking for

  • I moved from XP to Windows7 and want to keep all my old emails/files but have a new email address - how do I?

    I have my old profile saved in my cloud backup that I downloaded on my desktop. I also downloaded the latest version of Thunderbird and created a new e-mail address.

  • Satellite Pro A300 random freezes

    I had this problem for the last few weeks. Started during the game Football Manager 2011 and Windows Media Player in the background.Blue screen crash is came and never recovered from that, so I have it restored to its original condition and installed

  • Error message appears after using the recovery CD

    Hello I use recovery DVD-ROM. Her work very well but after restarting the laptop, it gives me error, missing some files.I try to fix that but still error. What can I do?If I sent him to the workshop, that it would take 7 days to 2 months. :(Last time

  • Plug MBP/VGA

    I have a MBP with a HDMI port, as well as the thunderbolt and usb ports. I need to connect it to a VGA projector, but AFAIK, the HDMI port will be only digital player displays so a HDMI/VGA adapter does not work. (more I notice that apple sell not on

  • Back panel Lenovo Z500

    I can't remove my rear panel to replace a bad fan. I remove all of the screws fixing and used some old credit cards to try to get it open, but alas, it won't come off. What is the problem?