How to calculate the cumulative sum  with certain interval in SAP HANA

Hi Experts,
I have a scenario in which I have a column with Boolean values and need to calculate the cumulative values in the interval where there are consecutive ones as mentioned below,
ID          Date                           column 1
1         9-14-2014 14:22:00          1
1          9-14-2014 14:25:00          1
2          9-14-2014 13:22:00          0
1          9-14-2014 15:02:00          0
1          9-14-2014 14:37:00          0
2          9-14-2014 14:25:00          1
2          9-14-2014 14:32:00          1
1          9-14-2014 14:05:00          1
2          9-14-2014 14:45:00          0
2          9-14-2014 14:59:00          0
1          9-14-2014 15:12:00          1
1          9-14-2014 15:18:00          1
1          9-14-2014 15:21:00          1
First needs to group by 'ID' and Order By ' Date' and calculate the calculated column 'cumulative sum' for the consecutive ones in 'column1' as
ID          Date                           column 1          Cumulative sum
1          9-14-2014 14:05:00          1                    1    
1          9-14-2014 14:22:00          1                    2
1          9-14-2014 14:25:00          1                    3
1          9-14-2014 14:37:00          0                    0
1          9-14-2014 15:02:00          0                    0
1          9-14-2014 15:12:00          1                    1
1          9-14-2014 15:18:00          1                    2
1          9-14-2014 15:21:00          1                    3
2          9-14-2014 13:22:00          0                    0
2          9-14-2014 14:25:00          1                    1
2          9-14-2014 14:32:00          1                    2
2          9-14-2014 14:45:00          0                    0
2          9-14-2014 14:59:00          0                    0
Is there any function and way to calculate this  without loops in procedure?
Please help!!. Thank you!!
-Gayathri

Hi Henrique,
The SQL,
SELECT id, date, col1, sum(col1) over (partition by col1 order by id, date) as cumulative_sum
will give me the cumulative value for all the col 1 with same value which is not my requirement,
the above will give the result as shown below. I have also added the correct values as per my requirement as well
ID          Date                           column 1          Cumulative sum     correct ***_sum
1          9-14-2014 14:05:00          1                    1                              1
1          9-14-2014 14:22:00          1                    2                              2
1          9-14-2014 14:25:00          1                   2                              3
1          9-14-2014 14:37:00          0                    0                              0
1          9-14-2014 15:02:00          0                    0                              0
1          9-14-2014 15:12:00          1                   4                              1
1          9-14-2014 15:18:00          1                   5                              2
1          9-14-2014 15:21:00          1                    6                              3
2          9-14-2014 13:22:00          0                    0                              0
2          9-14-2014 14:25:00          1                    1                              1
2          9-14-2014 14:32:00          1                    2                              2
for my requirement , I need to find the cumulative value only for the consecutive 1s if there are 0s in between then there should be a break and cumulative sum should start again for the next set of consecutive 1s
Hope you could help. Thanks.
-Gayathri

Similar Messages

  • How to calculate the individual sums of multiple columns in a single query

    Hello,
    Using Oracle 11gR2 on windows 7 client. I have a question on calculating sum() on multiple columns on different columns and store the results in a view. Unfortunately I could not post the problem here as it keeps on giving error "Sorry, this content is not allowed", without telling where or what it is! So I had to post it in the stack-overflow forum, here is the link: http://stackoverflow.com/questions/16529721/how-to-calculate-the-individual-sums-of-multiple-columns-in-a-single-query-ora
    Will appreciate any help or suggestion.
    Thanks

    user13667036 wrote:
    Hello,
    Using Oracle 11gR2 on windows 7 client. I have a question on calculating sum() on multiple columns on different columns and store the results in a view. Unfortunately I could not post the problem here as it keeps on giving error "Sorry, this content is not allowed", without telling where or what it is! So I had to post it in the stack-overflow forum, here is the link: http://stackoverflow.com/questions/16529721/how-to-calculate-the-individual-sums-of-multiple-columns-in-a-single-query-ora
    Will appreciate any help or suggestion.
    ThanksLooks like you want a simple group by.
    select
              yr
         ,      mnth
         ,      region
         ,     sum(handled_package)
         ,     sum(expected_missing_package)
         ,     sum(actual_missing_package)
    from test
    group by
         yr, mnth, region
    order by      
         yr, mnth, region;I wouldn't recommend storing your data for year / month in 2 columns like that unless you have a really good reason. I would store it as a date column and add a check constraint to ensure that the date is always the first of the month, then format it out as you wish to the client.
    CREATE TABLE test
         year_month                              date,
        Region                     VARCHAR2(50),
        CITY                       VARCHAR2(50),             
        Handled_Package            NUMBER,       
        Expected_Missing_Package   NUMBER,   
        Actual_Missing_Package     NUMBER
    alter table test add constraint firs_of_month check (year_month = trunc(year_month, 'mm'));
    ME_XE?Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
      2  Values (to_date('2012-nov-12', 'yyyy-mon-dd'), 'Western', 'San Fransisco', 200, 10, 5);
    Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
    ERROR at line 1:
    ORA-02290: check constraint (TUBBY.FIRS_OF_MONTH) violated
    Elapsed: 00:00:00.03
    ME_XE?Insert into TEST (year_month, REGION, CITY, HANDLED_PACKAGE, EXPECTED_MISSING_PACKAGE, ACTUAL_MISSING_PACKAGE)
      2  Values (to_date('2012-nov-01', 'yyyy-mon-dd'), 'Western', 'San Fransisco', 200, 10, 5);
    1 row created.
    Elapsed: 00:00:00.01
    ME_XE?select
      2        to_char(year_month, 'fmYYYY')    as year
      3     ,  to_char(year_month, 'fmMonth')   as month
      4     ,  Region
      5     ,  CITY
      6     ,  Handled_Package
      7     ,  Expected_Missing_Package
      8     ,  Actual_Missing_Package
      9  from test;
    YEAR         MONTH                REGION                         CITY                    HANDLED_PACKAGE EXPECTED_MISSING_PACKAGE ACTUAL_MISSING_PACKAGE
    2012         November             Western                        San Fransisco                       200                       10                      5
    1 row selected.
    Elapsed: 00:00:00.01
    ME_XE?Then you have nice a nice and easy validation that ensures you data integrity.
    Cheers,

  • How to change the unload priority of a table in SAP HANA?

    Hi Experts,
    How we can change the unload priority of a table in SAP HANA? I know by default the priority is 5. Is there any way so that we can check the unload priority of a particular table in HANA studio? Is there any SQL statement to get the same?
    Please suggest.
    Thanks in advance.
    Regards,
    Arindam

    Hello Arindam,
    Just for the future:
    ALTER TABLE - SAP HANA SQL and System Views Reference - SAP Library
    To check before hand:
    select
    table_name, unload_priority from SYS.TABLES
    where table_name = '<Your Table>'
    To Make the change:
    alter table <Your Table>unload priority <Priority You Want>.
    As you have asked in the BW on HANA section I assume you're on BW and you could also have checked this with tx SE14.
    Hopefully the above gives you everything you need.
    Kind Regards,
    Amerjit

  • MDX - how to calculate the sales between a certain period and the currentmember with a max date

    Hi all,
    1)I need to calculate the sales between W1 and the currentmember, but the max date should be W20. How can I solve this in MDX?
    MEMBER [Measures].[Q1 act.period to date sales] AS
    Sum(
    [Date invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[1]
    [Date invoice].[Bonus calendar - Week].Currentmember
    *[CurrentSalesPeriod]
    ,[Measures].[Sales amount]
    This is the measure I have, I need to add the max date is W20. 
    2)Also, when I calculate the sales between [Date invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[40] and the currentmember, for example being [Date invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[20],
    he calculated the sales between this period. However, in theory this is not possible because W20 < 40. Is there any way I can put a constraint on this that he only calculates the sales if the currentmember is > the start period?
    Thanks in advance!

    Y, you can put a check on to determine if currentmember > start period.
    Firstly, your period keys are composite of year and period (indicated by the two ampersands in the fully qualified date above). If your period keys were non-composite (eg 201501) it would be a simple matter of 
    IIF([Date invoice].[Bonus calendar - Week].Currentmember.properties("key") > [Date
    invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[1].properties("key"),[Measures].[Sales
    amount],null)
    There are advantages of having two period attributes on the dimension, one with a fully unique
    key and the other with just the period of fy, but that's digressing.
    With your composite keys, you could use a calc like the following for the SUM()
    Sum(
    [Date invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[1]
    [Date invoice].[Bonus calendar - Week].Currentmember
    *[CurrentSalesPeriod]
    ,IIF([Date
    invoice].[Bonus calendar - Week].Currentmember.parent.properties("key") +[Date
    invoice].[Bonus calendar - Week].Currentmember.properties("key") > [Date
    invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[1].parent.properties("key")+[Date
    invoice].[Bonus calendar - Week].[Bonus week of year].&[2015]&[1].properties("key"),[Measures].[Sales
    amount],null))
    Richard

  • How to find the cumulative Sum

    How to get the fastest result in 180 million records table to get the BTN(BillingTelephoneNbr) wise MonthlySalesCost.
    select B.BTN,b.cost,sum(a.cost) as cum_sal
    from Monthly_BTN a,Monthly_BTN b
    where a.rowid <= b.rowid
    group by b.rowid,b.BTN,b.cost.
    Above query is taking up the too much time.
    Option:-
    a) partition the table
    b) rebuild the index
    c) alter the index
    d) any other suggestion??
    Let me know the best way.

    I don't understand why you need a self-join.
    See the following example, with cumulative sum about deptno :
    SCOTT@demo102> ed
    Wrote file afiedt.buf
      1  select empno, deptno, sal, sum(sal) over (partition by deptno order by empno)
      2  from emp
      3* order by deptno , empno
    SCOTT@demo102> /
         EMPNO     DEPTNO        SAL SUM(SAL)OVER(PARTITIONBYDEPTNOORDERBYEMPNO)
          7782         10      24500                                       24500
          7839         10      50000                                       74500
          7934         10      13000                                       87500
          7369         20       8000                                        8000
          7566         20      29750                                       37750
          7788         20      30000                                       67750
          7876         20      11000                                       78750
          7902         20      30000                                      108750
          7499         30      16000                                       16000
          7521         30      12500                                       28500
          7654         30      12500                                       41000
          7698         30      28500                                       69500
          7844         30      15000                                       84500
          7900         30       9500                                       94000
    14 rows selected.
    SCOTT@demo102> So, your query would be :
    select BTN,cost,sum(cost) over (partition by BTN order by BTN,cost) as ***_sal
    from Monthly_BTN
    order by BTN,cost;Anyway, a query without where clause on a huge table (180 million records) seems to me like a little strange, are you sure that you will read all rows ?
    Nicolas.
    partition or not partition about BTN... like you want.
    Message was edited by:
    N. Gasparotto

  • How can I create geographic charts with Lumira connected to SAP HANA?

    Hello All
    I'm testing SAP Lumira and I want to make geographic charts with data from SAP HANA. This is possible when I use data from a local Excel-file. But not when I use data from SAP HANA. I was told it should work after installing Service Pack 8. But it still doesn't work. Maybe we have to turn something on, change a configuration file. Maybe it has to do with SAP Lumira, not with SAP HANA. Any help is greatly appreciated.
    Here are some technical details:
    SAP HANA Version: 1.00.81.00.392776 (NewDB100_REL).
    SAP Lumira Version: 1.19.0 (build 1099) (latest version).
    SAP HANA Studio Version: 1.00.81.0.
    Windows Version: Windows 7 Enterprise, Service Pack 1.
    Kind regards, Robbert

    Hello Antoine
    At paragraph 3.2.2 on page 73 it says:
    Creating geography hierarchies with latitude and longitude data is not currently supported for SAP HANA data sources.
    I think this explains everything. Well, almost everything. I created geographic charts with Lumira while connected with Excel or a csv-file. The question now is, am I waiting for an update of SAP Lumira or an update of SAP HANA? Or both?
    Kind regards, Robbert

  • How to calculate the total sum value of a particular field that repeats

    Hi All,
    I have the following Req...File----Idoc Scenario
    In the Inbound xml file i will get the Sales Order details with suppose 10 line items( 10 Orders)
    Each line item represents one one SO. So totally i wil have 10 Sales Orders in this file.
    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:MT_Sales_Order xmlns:ns0="http://sap/Sales_Order">
       <Header>
          <COMP_CODE></COMP_CODE>
          <DOC_TYPE></DOC_TYPE>
           <SUPPL_VEND></SUPPL_VEND>
       </Header>
       <Item>
          <ITEM></ITEM>
          <MATERIAL></MATERIAL>
          <PLANT></PLANT>
          <QUANTITY></QUANTITY>
          <Amount></Amount> 
    </Item>
    In the above structure Item Segment will repeats as many no. of Sales Orders comes in a file.
    In a file if there are 10 Orders means the Item segment wil repeats 10 times.
    I have the Amount field in the Item Segment, each and every time that needs to be added to next Amount value that presents in the next Line Item.
    Finally i will have the Another separate field caled Grand Total, and i have to get the total summation of the 10 values of the Amount field at last.
    Can we achieve this using UDF or is there any way to do this
    REgards

    Hi,
    Do like this, actually in your case sum is taking place before the condition check for discount type
    do a little change in mapping
    DiscntType--removeContext--
                                              EqulsS-------IfWithoutElse----Amount---removecontext--then---SUM-
    Connstatnt(Value)
    --->GrandAmount
    Krishna, Check the same question asked by Rajesh in thread Calculate totals of segments that occur multiple times
    Thanks!

  • How to calculate the phase difference between two square wave (acquired from two channel in one DAQ)

    Hello everyone ,
     I need a quickly help that as below :
    I am trying use PCI-6220 to acquired six signals from one rotation encoder (channel A, channel B,channel Z ,and their non-signals) .The encoder out signals will be square wave and 4000pulses per revolution. I set it rotation at 300rpm speed. I need show every square wave of six out channels in waveform and measure the A-B phase difference to check if the value is correct (designed value should be 90deg) .
    I have no idea how to measure or calculate the phase difference of two square wave base on synchronizate to acquire these two square wave from two channel on the PCI-6620....
    Anyone can give a idea how to calculate the phase difference with two square wave ?
    Thank a lot and Thanks again...
    Tim

    Tim,
    Here is a simple rising edge detector for one channel.
    Lynn
    Attachments:
    Rising edge.vi ‏15 KB

  • How to calculate the number of sent/received emails of a certain domain

    Thank you for what you have helped me with!
    How to calculate the number of sent/received emails of a certain domain in a certain period? It is Messaging Server 5.2, Directory Server 4.2. Is there a log option for this?
    Thank you.

    Not sure where you find, "LOG_MESSSAGE_ADD". I don't actually find this option in the documentation.
    The domains that mails are coming from and being sent to are certainly logged in the normal mail.log, so why mess with additional logging options? If you're talking about "LOG_CONNECTION", I actually see no additional data that is useful to you.
    If you decide to change the option.dat, you do indeed need to
    imsimta cnbuild (note, it's not cnrebuild)
    imsimta restart
    If I were facing the same issue, I'd be looking at the log parsing perl script, and simply modifying it to do what I wanted.

  • How to calculate the RMS level of a signal with its spectral representation

    hi!
    how to calculate the RMS level of a signal with its spectral representation
    thanks

    1. Find the length, N, of the spectral signal.
    2. Convolve the magnitude of the signal with itself.
    3. Take the Nth element of the resulting signal
    4. Take the square root.
    5. This should be the RMS value of your signal.
    Randall Pursley
    Attachments:
    RMS.bmp ‏616 KB

  • How to calculate a cumulative total in HANA

    Hello,
    I have a detail table (e.g. GL line item table) which has entries for 'posting date', account number, year, month, posting amount (single key figure) and account type (whether the account is a profit & loss account or a balance sheet account.
    The requirement is to create a view which shows balance of month for profit and loss accounts and cumulative balance per month for balance sheet accounts (i.e. total of all line items till end of the month, not just for the month).
    Currently I am able to get only balance for the month , but not the cumulative balance.
    How can I calculate a cumulative amount column/key figure - either through Analytical view or calculation view?
    Thanks,
    Ninad

    The Window Functions should help. Here is a sample query in SPS06 using a sum over function against an SAP HANA Live standard view.
    SELECT Year("BillingDocumentDate_E") as "Invoice_Year", "BillingDocumentDate_E" as "Invoice_Date", round("TotalNetAmount_E") as "Billed_Amount",
           round(Sum("TotalNetAmount_E") OVER (partition by Year("BillingDocumentDate_E") order by "BillingDocumentDate_E")) as "YTD_Amount"
    FROM "_SYS_BIC"."sap.hba.ecc/BillingDocumentQuery"
    WHERE "SAPClient" = '004' and "SoldToPartyName" = 'Becker Berlin' and "BillingDocumentDate_E" >= '2002-01-01'
    GROUP BY Year("BillingDocumentDate_E"), "BillingDocumentDate_E", "TotalNetAmount_E"
    ORDER BY 1, 2
    Message was edited by: Marc Daniau

  • How to calculate the variance in PO price history?

    hi
    i hav standard report ME1P, since i need to do some modifications in this program i copied to Y prgrm,,,'
    im getting all the values properly...
    but my prblm is im not getting how to calculate the variance? im not getting the logic behid it...
    can anybody expln me in breif plz....
    Regards
    Smitha

    Hi Venu,
    there is another way as well, however, you won't be able to see Variance and Variance% as a saperate keyfigure which is not under the period. Thats the reason I didn't mention it earlier.
    What you can do is, Drag the period to the columns, Now create a structure that has the two keyfigures std price, moving price. Here if you add Variance and Variance%, Then these CKF's would come under period as well, which you don't need. And the system won't allow you to add these CKF's outside this structure. The reason being because the interpretation of the data then wouldn't make any sense here. You can create a new structure as well with these CKF's but they would become a subpart of your previous structure.
    choose the required characteristics in the Rows section. Save it.
    Let us know, Sorry man, can't help u much at this problem.
    regards,
    Sree.

  • How to calculate the row size

    Hi,
    I am having a table of 4 column. First column is varchar2(10), Second column is Number(5), Third column is char(10) and Fourth column is Number(12,2).
    Now how to calculate the size of each row. I want to know how much space it will take for each row.
    Thanx & Regards,
    Swarup

    For a complete discussion of storage requirements for different datatypes see the Oracle 8i Concepts manual.
    All Oracle documentation is available on-line via the 'Documentation' link on the OTN home page.
    Your Col2 NUMBER(5) will take 4 bytes if it holds a 5 digit positive or negative number.
    Your Col4 NUMBER(12,2) will take 7 bytes if it holds a number with 12 digits that is positive and will take 8 bytes if it holds a number with 12 digits that is negative.
    The following is from the Concepts manual Chapter 12 (Copyright by Oracle)
    Built-In Datatypes 12-9
    Internal Numeric Format
    Oracle stores numeric data in variable-length format. Each value is stored in
    scientific notation, with one byte used to store the exponent and up to 20 bytes to
    store the mantissa. The resulting value is limited to 38 digits of precision. Oracle
    does not store leading and trailing zeros. For example, the number 412 is stored in a
    format similar to 4.12 x 10 2 , with one byte used to store the exponent (2) and two
    bytes used to store the three significant digits of the mantissa (4, 1, 2). Negative
    numbers include the sign in their length.
    Taking this into account, the column size in bytes for a particular numeric data
    value NUMBER ( p), where p is the precision of a given value, can be calculated
    using the following formula:
    ROUND((length( p)+ s)/2))+1
    where s equals zero if the number is positive and s equals 1 if the number is
    negative.
    Zero and positive and negative infinity (only generated on import from Version 5
    Oracle databases) are stored using unique representations. Zero and negative
    infinity each require one byte; positive infinity requires two bytes.
    null

  • How can I restrict a vendor with certain value limit?

    Hi Gururs,
    How can I restrict a vendor with certain value limit?.
    Scenario is like this
    If my company was decided to purchase goods from a particular vendor upto Rs.1000, if cross the rs.1000 limit don't allow the Posting the PO and get the Message as warning/error.
    Give the configuration setting's and T.codes
    Thanks and regards
    G.N.Rao

    Hi
    Go to T.Code oms4 and then select the material status BP (Blocked for purchasing)
    Click on Details
    In that under Purchasing select the option A= Warning or B=Error
    Click on Save
    Thus by doing this no further purchasing function for that material can be done. So the PO can not be issued
    So as and when the value limit reaches see that purchasing option is blocked
    So no further PO are generated in the future
    I hope this helps you out
    If found useful reward accordingly
    Thanks
    pavan

  • In labview,how to calculate the parameters of the bilinear model that simulates the generation electroencephalogram?

    Dear Sir or madam:
    I have collected a lot of electroencephalogram data, which looks like a continuous sine wave plus a noise signal. what i want to do now is to fit the data with the bilinear model as the attached jpg file. the problem is the parameters of the model are changing with the evolution of the signal. would someone like to tell me how to calculate the parameters in the bilinear model?
    thank you very much
    Attachments:
    Bilinear_Model.jpg ‏33 KB

    I can point you in the right direction, but probably not answer your question really well. If you have the pro or full distribution of LabVIEW, you can use the curve fitting VIs to match any set of data to a theoretical curve. The curve fitting Express VI may do all you need to do (use the nonlinear option). To get the signal evolution, break your data into pieces and process each. If you need to average a bit, you can use a sliding window of your data for each analysis, moving the window less than the window width for each analysis.
    There are a plethora of curve fitting techniques built into LabVIEW - matrix operations, linear and log linear fits, Levenberg-Marquardt methods, downhill simplex, etc. You will probably need to experiment a bit to get a stable
    algorithm for your case. Check your literature for ways other people have done this. There may be an easy, stable method out there.
    If you are unfamiliar with curve fitting techniques, or you do not have the pro or full versions of LabVIEW, I would recommend "Numercial Recipes in C" by Press et. al., published by Cambridge University Press. The chapter on Modeling of Data will get you going. The rest of the book will provide any background you need.
    This was a very general answer. If you need something more specific, let me know. Be aware that this type of problem usually requires some trial and error to get right. The answers should be tightly scrutinized before being believed.
    This account is no longer active. Contact ShadesOfGray for current posts and information.

Maybe you are looking for