A function instead of UNBOUNDED PRECEDING (like "Last field with value=0")

Hello,
I have a table with many rows. The attributes of the table are code, month and value.
For each code there are 12 months and 12 values.
No I want to add the gaps between the months...
Is it possible to count the following gaps between the different rows...?
For example:
Original table1:_
code, month, value
1,1,20
1,2,0
1,3,30
1,4,0
1,5,40
1,6,0
1,7,0
1,8,20
1,9,0
1,10,10
1,11,0
1,12,0
5,1,0
5,2,20
5,3,10
description:*
january value = 20
february value = 0 (=>count this gap => new value 1 )
march value = 30
april value = 0 (=>count this gap => new value 1 )
may value = 40
june value = 0
july value = 0 (=>count this two following gaps => new value 2 )
agust value = 20
september value = 0 (=>count this gap => new value 1 )
october value = 10
november value = 0
december value = 0 (=>count this two following gaps => new value 2 )
New target table:_
code, month, value
1,1,20
1,2,*1*
1,3,30
1,4,*1*
1,5,40
1,6,0
1,7,*2*
1,8,20
1,9,*1*
1,10,10
1,11,0
1,12,*2*
5,1,*1*
5,2,20
5,3,10
I tried this code:
select code, month
sum(value) over (
order by month
rows between unbounded preceding and current row
*) as final_value*
from table1 order by month;
This adds all following fields cumulative from beginning to current_row. But I need this adding only for the following gaps... then start with countin by 0.
I need only the following like in the example on top. Maybe is there an other function like decode to count only the following gaps...!?
A function instead of unbounded preceding....like "*Last field with value=0*" or something... ?
Best regards,
Tim

TimB83 wrote:
I have a table with many rows. The attributes of the table are code, month and value.
For each code there are 12 months and 12 values.
No I want to add the gaps between the months...
Is it possible to count the following gaps between the different rows...?
For example:
Original table1:_
code, month, value
1,1,20
1,2,0
1,3,30
1,4,0
1,5,40
1,6,0
1,7,0
1,8,20
1,9,0
1,10,10
1,11,0
1,12,0
5,1,0
5,2,20
5,3,10
description:*
january value = 20
february value = 0 (=>count this gap => new value 1 )
march value = 30
april value = 0 (=>count this gap => new value 1 )
may value = 40
june value = 0
july value = 0 (=>count this two following gaps => new value 2 )
agust value = 20
september value = 0 (=>count this gap => new value 1 )
october value = 10
november value = 0
december value = 0 (=>count this two following gaps => new value 2 )
New target table:_
code, month, value
1,1,20
1,2,*1*
1,3,30
1,4,*1*
1,5,40
1,6,0
1,7,*2*
1,8,20
1,9,*1*
1,10,10
1,11,0
1,12,*2*
5,1,*1*
5,2,20
5,3,10
...Tim,
you should post this question on the "SQL and PL/SQL" forum since it's a typical SQL question.
There are probably much better ways to accomplish this and the guys over there will tell you, but here are two examples that might get you started:
1. Pre-10g without MODEL clause
with t as (
select 1 as code, 1 as month, 20 as value from dual union all
select 1 as code, 2 as month, 0 as value from dual union all
select 1 as code, 3 as month, 30 as value from dual union all
select 1 as code, 4 as month, 0 as value from dual union all
select 1 as code, 5 as month, 40 as value from dual union all
select 1 as code, 6 as month, 0 as value from dual union all
select 1 as code, 7 as month, 0 as value from dual union all
select 1 as code, 8 as month, 20 as value from dual union all
select 1 as code, 9 as month, 0 as value from dual union all
select 1 as code, 10 as month, 10 as value from dual union all
select 1 as code, 11 as month, 0 as value from dual union all
select 1 as code, 12 as month, 0 as value from dual union all
select 5 as code, 1 as month, 0 as value from dual union all
select 5 as code, 2 as month, 20 as value from dual union all
select 5 as code, 3 as month, 10 as value from dual
r1 as (
select
        case
        when value = 0
        then 1
        else 0
        end as is_gap
      , case
        when value != 0
        then rownum
        else null
        end as grp_info
      , code
      , month
      , value
from
        t
r2 as (
select
        last_value(grp_info ignore nulls) over (partition by code order by month) as grp
      , is_gap
      , code
      , month
      , value
from
        r1
select
        code
      , month
      , case
        when value = 0
        and (lead(value) over (partition by code order by month) != 0 or
             lead(value) over (partition by code order by month) is null)
        then sum(is_gap) over (partition by code, grp)
        else value
        end as value
from r2;2. 10g and later with MODEL clause
with t as (
select 1 as code, 1 as month, 20 as value from dual union all
select 1 as code, 2 as month, 0 as value from dual union all
select 1 as code, 3 as month, 30 as value from dual union all
select 1 as code, 4 as month, 0 as value from dual union all
select 1 as code, 5 as month, 40 as value from dual union all
select 1 as code, 6 as month, 0 as value from dual union all
select 1 as code, 7 as month, 0 as value from dual union all
select 1 as code, 8 as month, 20 as value from dual union all
select 1 as code, 9 as month, 0 as value from dual union all
select 1 as code, 10 as month, 10 as value from dual union all
select 1 as code, 11 as month, 0 as value from dual union all
select 1 as code, 12 as month, 0 as value from dual union all
select 5 as code, 1 as month, 0 as value from dual union all
select 5 as code, 2 as month, 20 as value from dual union all
select 5 as code, 3 as month, 10 as value from dual
select
         code
       , month
       , value
from
         t
model
partition by (code)
dimension by (month)
measures (value, 0 as gap_cnt)
rules (
  gap_cnt[any] order by month =
  case
  when value[cv() - 1] = 0
  then gap_cnt[cv() - 1] + 1
  else 0
  end,
  value[any] order by month =
  case
  when value[cv()] = 0 and presentv(value[cv() + 1], value[cv() + 1], 1) != 0
  then presentv(gap_cnt[cv() + 1], gap_cnt[cv() + 1], gap_cnt[cv()] + 1)
  else value[cv()]
  end
);Regards,
Randolf
Oracle related stuff blog:
http://oracle-randolf.blogspot.com/
SQLTools++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676/
http://sourceforge.net/projects/sqlt-pp/

Similar Messages

  • Last cell with value in excel

    Hi all. I'm on devsuite 10g.
    Opening an excel file is there a way to obtain the last cell with value in a sheet??
    Thanks all for the collaboration,
    Fabrizio

    Hi all. I'm on devsuite 10g.
    Opening an excel file is there a way to obtain the last cell with value in a sheet??
    Thanks all for the collaboration,
    Fabrizio

  • Analytical function count(*) with order by Rows unbounded preceding

    Hi
    I have query about analytical function count(*) with order by (col) ROWS unbounded preceding.
    If i removed order by rows unbouned preceding then it behaves some other way.
    Can anybody tell me what is the impact of order by ROWS unbounded preceding with count(*) analytical function?
    Please help me and thanks in advance.

    Sweety,
    CURRENT ROW is the default behaviour of the analytical function if no windowing clause is provided. So if you are giving ROWS UNBOUNDED PRECEDING, It basically means that you want to compute COUNT(*) from the beginning of the window to the current ROW. In other words, the use of ROWS UNBOUNDED PRECEDING is to implicitly indicate the end of the window is the current row
    The beginning of the window of a result set will depend on how you have defined your partition by clause in the analytical function.
    If you specify ROWS 2 preceding, then it will calculate COUNT(*) from 2 ROWS prior to the current row. It is a physical offset.
    Regards,
    Message was edited by:
    henryswift

  • Please help me on UNBOUNDED PRECEDING and CURRENT ROW

    Helllo Experts,
    Kindly help me on below query..
    SELECT
       SUM(n) AS month_amount,
      SUM(SUM(n)) OVER
        (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
        AS cumulative_amount
      FROM t1
      GROUP BY n
      ORDER BY n;Here i need explanation over(what it does) over() and (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)And if you have any Docs over ANALYTICAL functions please help with them.
    Ur help ll be appreciated..
    Thanks
    Basav

    http://download.oracle.com/docs/cd/E11882_01/server.112/e26088/functions004.htm#SQLRF06174
    Regards
    Etbin

  • BO Advance analysis 1.1.4,  Info Field like Last Data Update Table

    Hi Experts,
    I am using BO Advance analysis 1.1.4 for reporting output. There we have option to show Info Field like Last Data Update.
    It is using this function to get that info :
    =SAPGetSourceInfo("DS_1", "LastDataUpdate")
    Anybody have idea what BW table it is refering to get that info.
    As per my understanding, since these workbook of Adv. Analysis are refering Query which are built on MultiProviders, it go and find out the last cube/dso successful data load.
    Also I manually checked the date and time of last successful update in cube, but it is not matching as far as time is concern. Date is matching.
    Please help me to get the correct table which gave that info (date and time).
    Thank-You.
    Regards,
    Vinod

    Hi Tammy,
    I was trying for the similar requirement to get the "Last refreshed time" of the table used in the calculation  view  in HANA.
    It uses the same formula : =SAPGetInfoLabel("LastDataUpdate")
    But since Analytic View actually doesn't store any data, it is virtual. The date am seeing is the last changed date. i.e if i do any changes on the model, am seeing the last date when I changed it.
    Is there any function which we can use to get the Last refreshed date of the table in the model am using?
    Regards,
    Krishna Tangudu

  • Is there any functionality for AVERAGE in ALV, like do_sum, subtot?

    Hi Experts,
    In my_alv report, am doing sub/totals for prices, by using do_sum, subtot functions.........fine. But, I need to do/display the AVERAGE value for Discount % column?
    Is there any functionality for AVERAGE in ALV, like do_sum, subtot?
    thanq

    hi
    check these links out
    http://www.sapfans.com/forums/viewtopic.php?t=20386
    http://www.sapfans.com/forums/viewtopic.php?t=85191
    http://www.sapfans.com/forums/viewtopic.php?t=88401
    http://www.sapfans.com/forums/viewtopic.php?t=17335
    regards
    vijay
    reward points if helpful

  • Hello..i use firefox 4 beta 05..my question is..why when i open let's say 6 tabs..my windows show that i have 6 instancies of firefox instead of one like it does with firefox 3.6? thanks

    hello..i use firefox 4 beta 05..my question is..why when i open let's say 6 tabs..my windows show that i have 6 instancies of firefox instead of one like it does with firefox 3.6? thanks
    maybe i am saying it wrong..when i press alt+tab there is only one firefox open..but in windows 7 in the taskbar..every tab open..appears as another window open

    If you are referring to taskbar previews, you can turn them off by modifying a hidden preference.
    # Type '''about:config''' into the location bar and press enter
    # Accept the warning message that appears, you will be taken to a list of preferences
    # In the filter box type '''previews'''
    # Double-click on the preference browser.taskbar.previews.enable to change its value to '''false'''

  • Can i use an analytic function instead of a group by clause?

    Can i use an analytic function instead of a group by clause? Will this help in any performance improvement?

    analytic can sometimes avoid scanning the table more than once :
    SQL> select ename,  sal, (select sum(sal) from emp where deptno=e.deptno) sum from emp e;
    ENAME             SAL        SUM
    SMITH             800      10875
    ALLEN            1600       9400
    WARD             1250       9400
    JONES            2975      10875
    MARTIN           1250       9400
    BLAKE            2850       9400
    CLARK            2450       8750
    SCOTT            3000      10875
    KING             5000       8750
    TURNER           1500       9400
    ADAMS            1100      10875
    JAMES             950       9400
    FORD             3000      10875
    MILLER           1300       8750
    14 rows selected.
    Execution Plan
    Plan hash value: 3189885365
    | Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT   |      |    14 |   182 |     3   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE    |      |     1 |     7 |            |          |
    |*  2 |   TABLE ACCESS FULL| EMP  |     5 |    35 |     3   (0)| 00:00:01 |
    |   3 |  TABLE ACCESS FULL | EMP  |    14 |   182 |     3   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - filter("DEPTNO"=:B1)which could be rewritten as
    SQL> select ename, sal, sum(sal) over (partition by deptno) sum from emp e;
    ENAME             SAL        SUM
    CLARK            2450       8750
    KING             5000       8750
    MILLER           1300       8750
    JONES            2975      10875
    FORD             3000      10875
    ADAMS            1100      10875
    SMITH             800      10875
    SCOTT            3000      10875
    WARD             1250       9400
    TURNER           1500       9400
    ALLEN            1600       9400
    JAMES             950       9400
    BLAKE            2850       9400
    MARTIN           1250       9400
    14 rows selected.
    Execution Plan
    Plan hash value: 1776581816
    | Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT   |      |    14 |   182 |     4  (25)| 00:00:01 |
    |   1 |  WINDOW SORT       |      |    14 |   182 |     4  (25)| 00:00:01 |
    |   2 |   TABLE ACCESS FULL| EMP  |    14 |   182 |     3   (0)| 00:00:01 |
    ---------------------------------------------------------------------------well, there is no group by and no visible performance enhancement in my example, but Oracle7, you must have written the query as :
    SQL> select ename, sal, sum from emp e,(select deptno,sum(sal) sum from emp group by deptno) s where e.deptno=s.deptno;
    ENAME             SAL        SUM
    SMITH             800      10875
    ALLEN            1600       9400
    WARD             1250       9400
    JONES            2975      10875
    MARTIN           1250       9400
    BLAKE            2850       9400
    CLARK            2450       8750
    SCOTT            3000      10875
    KING             5000       8750
    TURNER           1500       9400
    ADAMS            1100      10875
    JAMES             950       9400
    FORD             3000      10875
    MILLER           1300       8750
    14 rows selected.
    Execution Plan
    Plan hash value: 2661063502
    | Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT     |      |    14 |   546 |     8  (25)| 00:00:01 |
    |*  1 |  HASH JOIN           |      |    14 |   546 |     8  (25)| 00:00:01 |
    |   2 |   VIEW               |      |     3 |    78 |     4  (25)| 00:00:01 |
    |   3 |    HASH GROUP BY     |      |     3 |    21 |     4  (25)| 00:00:01 |
    |   4 |     TABLE ACCESS FULL| EMP  |    14 |    98 |     3   (0)| 00:00:01 |
    |   5 |   TABLE ACCESS FULL  | EMP  |    14 |   182 |     3   (0)| 00:00:01 |
    -----------------------------------------------------------------------------So maybe it helps

  • Function module that can give the last value or the highest value of a key

    hi,
    Is there any function module that can give the last value or the highest value of a key feild in a z table.
    regards,
    johnson

    Hi ,
    We have  aggregate functions in SQL. Some of the functions are as follows.
    MAX(col ) Determines the maximum value of the value in the column col in the resulting set or in the current group.
    MIN( col ) Determines the minimum value of the content of the column col in the resulting set or in the current group.
    AVG(  col ) Determines the average value of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
    SUM( col ) Determines the sum of the content of the column col in the resulting set or in the current group. The data type of the column has to be numerical.
    COUNT( col ) Determines the number of different values in the column col in the resulting set or in the current group.
    For further details , type the function name name and press F1 for further help.
    Eg: select count(mantr) from mara into workarea where condition.
    Reward points if helpful.
    Thanks and Regards.

  • When adjusting the audio volume in a selected region, why does it give me a curved line instead of straight and why cant I change it like i can with a fade in our out?

    When adjusting the audio volume in a selected region, why does it give me a curved line as a transition instead of straight, and why cant I change the fade shape like i can with a fade in our out? When I slide thet volume all the way down it makes a very abrupt down curve. It's audibly noticdible.
    Thank you.

    Use the range selection tool and select the portion of the clip you want to change the volume on. Drag the volume line down and FCP will create keyframes which you can adjust as you want to get a fade shape.

  • How to make tcp have establish option's function or reflexive in ASA like icmp have echo reply

    how to make tcp have establish option's function or reflexive in ASA like icmp have echo reply
    rather than permit tcp in both side

    An ASA firewall is stateful.
    The reflexive access for TCP connections (or UDP flows) is allowed by default as the firewall checks for established connections prior to applying an access-list on traffic that arrives at an interface.
    Excerpted from this document (emphasis mine):
    "Here are the individual steps in detail:
    Packet is reached at the ingress interface.
    Once the packet reaches the internal buffer of the interface, the input counter of the interface is incremented by one.
    Cisco ASA will first verify if this is an existing connection by looking at its internal connection table details. If the packet flow matches an existing connection, then the access-control list (ACL) check is bypassed, and the packet is moved forward.
    If packet flow does not match an existing connection, then TCP state is verified. If it is a SYN packet or UDP packet, then the connection counter is incremented by one and the packet is sent for an ACL check. If it is not a SYN packet, the packet is dropped and the event is logged."

  • Hi, im currently using an windows 8 computer and have installed itunes 11.5. My problem is i'm trying to transfer one song from my computer to my iphone. But some how i cannot drag and drop like last time itunes 10.x.

    Hi, im currently using an windows 8 computer and have installed itunes 11.5. My problem is i'm trying to transfer one song from my computer to my iphone. But some how i cannot drag and drop like last time itunes 10.x. I do not wish to sync as it means of deleting my entire song list from my phone just to transfer 1.

    " I ordered the Snow Leopard software and tried installing it, but it stopped and said to clean the disk."
    First off, your installation disc is in all likelihood faulty.
    This has been a known issue in the past:
    Faulty Snow Leopard install discs - Apple has no timeline on ...
    http://store.apple.com/us/help/returns_refund#policy:
    *You can return software, provided that it has not been installed on any computer. Software that contains a printed software license may not be returned if the seal or sticker on the software media packaging is broken.

  • HT4623 So just like last time I tried to update my iPhone software...MY PHONE IS FROZEN.  Displaying  iTunes icon and a USB cable I pressed the Power button and the menu botton together until the apple icon appeared held them down for 10 secs & NOTHING No

    So just like last time I tried to update my iPhone software...MY PHONE IS FROZEN.  Displaying  iTunes icon and a USB cable I pressed the Power button and the menu botton together until the apple icon appeared held them down for 10 secs & NOTHING Now what?  Do I just wait for the battery to run out?

    Hi Careesa,
    Your iPhone is in recovery mode-- you need to connect it to your computer and, most likely, iTunes will ask you to restore it. If you recently backed up this shouldn't be too much of a problem.

  • HT1386 Lost iPhone 4, bought iPhone 5, would like to transfer backup from last synch with computer but don't have the password

    Lost iPhone 4, bought iPhone 5, would like to transfer backup to new iPhone 5 from last synch with computer but don't have the password.

    What password? Did you encrypt your backup & now can't remember the encrypted backup passcode? If so, no, you won't be able to use that backup. If on a Mac, & you elected to save the passcode to your Keychain, you can recover it from that. Otherwise, you'll have to delete the backup. The data is unrecoverable.
    Or, are you talking about the iPhone passcode?

  • After I installed 7 on my IPad none of the keyboard functions work properly.  seems like microsft had something to do with this new operating system.  any know what I did wrong

    after I installed 7 on my IPad none of the keyboard functions work properly.  seems like microsft had something to do with this new operating system.  any know what I did wrong

    Thanks for the quick advice!
    Unfortunately, I am still unable to open my core productivity apps, namely Skype and Evernote. Twitter, as well, could not start. But interesting quick fix though, I never knew you could do that.
    Here's the error log after the restart:
    What else should I try?

Maybe you are looking for

  • Dvds can no longer be read on MacPro

    DVD drive used to play Movie DVDs fine  but recently stopped - on inserting a DVD now, machine clicks for a while then ejects the DVD. Inserting a CD (music) or data is fine - disc is accepted and available for access. Any ideas why just the DVD reje

  • Can't install win 7 using boot camp in Lion

    Hi ! help please!! i can't install win 7 (any version)  on my MBP 13", i'm running Lion and that's when it all started! had no problem  on SL using boot camp and win 7 but now my win 7 installation just stops on last install step screen and that scre

  • I cannot generate a table of contents in INDesign3

    Hi There: I'm fairly new to Indesign CS3 and I need to generate a table of contents. I created a book file which consists of 20 chapters. I also created a Frontmatter file, which is also part of the book file. According to the help menu, I should ope

  • In a java applet , how to call other jar file?

    i have i programm, this programm is a java application, also, i already put these program into jar file, and then if i double this jar file , its work! but , now i need to run this program in web browser, i thing that i will write a simple applet wit

  • Importing vinyl into my iBook

    Can someone give me an efficient method of converting my records to digital? I have an iBook with no audio in, but I do have an MAudio audio interface (USB)which I believe I can use. Any advice is appreciated. Chuck