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,

Similar Messages

  • 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

  • How to calculate the total of a calculated column in a list view at the end of the view?

    I have a view with the following columns ProductName, Quantity, Price, Total The total column is a calculated column which is the product of quantity and price. I want to place the sum of the total column by the end of the list view. I can do this with
    the price and quantity but not with the total column. how do I do this?

    You can use SharePoint Designer and calculate the total in xslt view. Refer to the following post for more information
    http://community.bamboosolutions.com/blogs/bambooteamblog/archive/2009/04/24/how-to-total-calculated-columns-in-a-sharepoint-list.aspx
    http://blog.metrostarsystems.com/2012/12/03/jennys-sharepoint-tip-sum-calculated-columns/
    Cheers,

  • 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 Get the min,max and original values in a single query

    Hi,
    I have a task where in i have to the min , max and the original values of  a data set .
    I have the data like below and i want the target as well as mentioned below
    SOURCE
    DATASOURCE
    INTEGRATIONID
    SLOT_DATE
    SLOT1
    SLOT2
    SLOT3
    SLOT4
    SLOT5
    SLOT6
    SLOT7
    SLOT8
    SLOT9
    SLOT10
    1
    101
    201111
    100
    100
    200
    100
    100
    100
    300
    300
    300
    300
    1
    101
    2011112
    200
    200
    200
    200
    100
    100
    100
    100
    200
    300
    TARGET
    DATASOURCE
    INTEGRATIONID
    SLOT_DATE
    SLOT_VALUE
    SLOT MIN
    SLOT_MAX
    SLOT NUMBER
    1
    101
    201111
    100
    1
    2
    SLOT1
    1
    101
    201111
    100
    1
    2
    SLOT2
    1
    101
    201111
    200
    3
    3
    SLOT3
    1
    101
    201111
    100
    4
    6
    SLOT4
    1
    101
    201111
    100
    4
    6
    SLOT5
    1
    101
    201111
    100
    4
    6
    SLOT6
    1
    101
    201111
    300
    7
    10
    SLOT7
    1
    101
    201111
    300
    7
    10
    SLOT8
    1
    101
    201111
    300
    7
    10
    SLOT9
    1
    101
    201111
    300
    7
    10
    SLOT10
    1
    101
    2011112
    200
    1
    4
    SLOT1
    1
    101
    2011112
    200
    1
    4
    SLOT2
    1
    101
    2011112
    200
    1
    4
    SLOT3
    1
    101
    2011112
    200
    1
    4
    SLOT4
    1
    101
    2011112
    100
    5
    8
    SLOT5
    1
    101
    2011112
    100
    5
    8
    SLOT6
    1
    101
    2011112
    100
    5
    8
    SLOT7
    1
    101
    2011112
    100
    5
    8
    SLOT8
    1
    101
    2011112
    200
    9
    9
    SLOT9
    1
    101
    2011112
    300
    10
    10
    SLOT10
    e
    so basically i would first denormalize the data using the pivot column and then use min and max to get the slot_start and slot_end.
    But then i
    can get the min and max ... but not the orignal values as well.
    Any thoughts would be appreciated.
    Thanks

    If you want to end up with one row per slot per datasource etc, and you want the min and max slots that have the same value as the current slot, then you probably need to be using analytic functions, like:
    with t as
    (SELECT 1 datasource,101    INTEGRATIONID, 201111     slotdate, 100    SLOT1, 100        SLOT2,    200    slot3, 100    slot4, 100    slot5, 100    slot6, 300    slot7, 300    slot8, 300    slot9, 300 slot10 FROM DUAL  union all
    SELECT 1,    101,    2011112,    200,    200,    200,    200,    100,    100,    100,    100,    200,    300 FROM DUAL),
    UNPIVOTED AS
    (SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,1 SLOT,SLOT1 SLOT_VALUE
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,2 SLOT,SLOT2
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,3 SLOT,SLOT3
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,4 SLOT,SLOT4
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,5 SLOT,SLOT5
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,6 SLOT,SLOT6
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,7 SLOT,SLOT7
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,8 SLOT,SLOT8
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,9 SLOT,SLOT9
    FROM T
    UNION ALL
    SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,10 SLOT,SLOT10
    FROM T)
    select DATASOURCE,INTEGRATIONID,SLOTDATE,slot,slot_value,min(slot) OVER (partition by datasource,integrationid,slotdate,rn) minslot,
        max(slot) OVER (partition by datasource,integrationid,slotdate,rn) maxslot
    FROM   
      select DATASOURCE,INTEGRATIONID,SLOTDATE,max(rn) over (partition by datasource,integrationid,slotdate order by slot) rn,slot,slot_value
      FROM
        (SELECT DATASOURCE,INTEGRATIONID,SLOTDATE,slot,slot_value,
              case when row_number() over (partition by datasource,integrationid,slotdate order by slot) = 1 or
              lag(slot_value) over (partition by datasource,integrationid,slotdate order by slot) <> slot_value
                  then row_number() over (partition by datasource,integrationid,slotdate order by slot)
                  ELSE null
                  END rn
        from unpivoted
    order by DATASOURCE,INTEGRATIONID,SLOTDATE,slot 

  • How to calculate the HFM Cube size in SQL Server-2005

    Hi
    How to calculate the HFM Cube size in SQL Server-2005 ?
    Below query used for Oracle. Then what is query for SQL Server?
    SQL> select sum(bytes/1024/1024) from dba_segments where segment_name like 'FINANCIAL_%' and owner='HFM';
    SUM(BYTES/1024/1024)
    SQL> select sum(bytes/1024/1024) from dba_segments where segment_name like 'HSV FINANCIAL%' and owner='HFM';
    SUM(BYTES/1024/1024)
    Regards
    Smilee

    What is your objective? The subcube in HFM is a concept which applies to the application tier - not so much to the database tier. The size of the subcube is the unique number of data strips (data values for January - December inclusive, for example) for the given entity, currency triplet or Parent.Child node. You have to account for parent accounts and customs which don't exist in the database but are generated in RAM in the application tier.
    So, if your objective is to find the largest subcubes, you could do this by querying the database and counting the number of records per entity/value (DCE tables) or parent.child entity combination (DCN tables). I'm not versed in SQL, but I think the script below would just tell you the schema size and not the subcube sizes.
    Check out Accelatis.com for a third party software product that can do this for you. The feature is called the Subcube Analyzer and was written by the same team that wrote HFM, so they ought to know how this works :-)
    --chris                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to calculate the switching score

    Dear Experts,
    As per ISO 2859, the normal inspection can be changed to reduced Inspection if the production is steady, current value of switching score is 30, approved by responsible authority. My request is how to calculate the switching score?. I follow G-I inspections and AQLs 1.5.
    Regards,
    Krish

    Hi Sujit,
    Thanks for your reply.
    I have find work arround for switching score. but my problem is we are applying switching rule to multiple sample,
    for that as per ISO if the 4th sample is used score will be 0 but the batch is accpeted, as per dynamic modification set up this consider as OK lot but as per the ISO below30 score we will not go for next reduced inspection. if it is rejected then we will follow work arround. How to control in DMR? Is there any user exit is available .
    regards,
    krish

  • How to calculate the previous year YTD balance in profit & loss statement

    Dear all,
    I would like to seek for advice on how to calculate the previous year YTD (Year to date) balance in profit & loss statement
    For example, if I enter May 2009, the YTD value should be a sum up of value from Jan to May.
    I have tried the current year YTD could be set in column selection dimension "financial period"
    FACT PER( Code = YearFirst(@Per):@Per )  Order By PER.Code Descending
    Then, I tried the previous year YTD in another column using
    FACT PER( Code = YearFirst(@Per-12):@Per-12 )  Order By PER.Code Descending
    But it failed -> Abnormally display three column, while the financial period I enter 200903 in parameter @Per
    Would anyone kindly help me on that?
    Regards,
    Simon Chiu

    Dear Jim,
    Thanks for your reply.
    However, the problems still cannot be solved.
    My expected output is 1 column listing the YTD value from previous year.
    I have tried two times using both the Code = YTD(@Per-12) and Code = YearFirst(@Per)-12:@Per respectively. However, the Excel display various column.
    I am using Financial Period 200903. In the last three column, it display the data in Financial Period 200901, 200902 and 200903 respectively.
    For the current year, the YTD is correct and show 1 column listing the sum of account in the Financial Period 200901, 200902 and 200903
    Would you kindly advice the code setting and how to set? Also, how I can access the syntax builder?
    Regards,
    Simon Chiu

  • How to calculate the unit for RATE?

    Hey All,
    I am not sure if there is something standard for this or not.
    I am calculating the 'Rate' by using 'Value/Amount' and 'Quantity' as follows -
    Rate == Value /  Quantity
    I need to calculate the unit for the rate as below -
    Rate unit == Value unit (Currency) /  Quantity unit (Base_uom) 
    (for example -
    if value is 1000 USD and quantity is 10 TO then Rate should come out as 100 USD / TO)
    Could anyone please suggest how to calculate the unit in this case?
    Many Thanks!
    Tanu

    Hi,
    Go through the below link it may give some idea
    http://help.sap.com/saphelp_nw04/Helpdata/EN/19/1d7abc80ca4817a72009998cdeebe0/content.htm
    Regards,
    Marasa.

  • How to calculate the Current APC (Acquisition and Production Cost)

    Hi,
    Please help me how to calculate the Current APC.
    The Current APC (Acquisition and Production Cost) is a calculated value based on Previous Year Acquisition balance plus any value changes up to the time of the report.
    The Asset History Report (RAGITT_ALV01) calculates the Current APC value &
    The Current APC can also be found in the Asset Explorer (transaction code AW01N) under Country Book 10/ Posted Values tab then the line “Acquisition Value” and column “Posted values”.
    I suppose that the calculation of Current APC (Acquisition and Production Cost) is getting done in the GET statements in the report RAGITT_ALV01, but unable to find the actual logic.
    Please help me.
    Thanks in advance,
    Satish

    Hi,
    you'll find the logic in fm FI_AA_VALUES_CALCULATE
    A.

  • How to calculate the Current APC

    Hi,
    Please help me how to calculate the Current APC.
    The Current APC (Acquisition and Production Cost) is a calculated value based on Previous Year Acquisition balance plus any value changes up to the time of the report.
    The Asset History Report (RAGITT_ALV01) calculates the Current APC value &
    The Current APC can also be found in the Asset Explorer (transaction code AW01N) under Country Book 10/ Posted Values tab then the line “Acquisition Value” and column “Posted values”. 
    Thanks in advance,
    Satish

    Hi,
    I suppose that the calculation of Current APC (Acquisition and Production Cost) is getting done in the GET statements in the report RAGITT_ALV01, but unable to find the actual logic where it is being done.
    Please help me.
    Thanks in advance,
    Satish

  • How to calculate the Daily Call Volume

    Hello,
    Can anyone please advise how to calculate the daily call volume in a contact center - the counts of the calls terminated in ICM ?
    Is there any webview report or a SQL query which provides the count ?
    Many thanks in advance for the help!
    Thanks & Regards,
    Naresh

    The ICM software generates a Termination_Call_Detail record for each call that arrives at the peripheral. From This report you can get the number of calls to ICM as well as the call details.
    To get the report run this sql query
    select * from dbo.t_Termination_Call_Detail where convert (varchar(10), DateTime, 101) = '12/03/2010'

  • 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 quota base quantity in quota arrangement?

    Hi all,
    As we all know, when a new supplier is added in the quota arrangment, we take the help of quota base qunatity so that the new supplier does not get any unfair advantage over the existing suppliers. Would you please help me on how to calculate the quota base quantity or on what basis the quota base quantity is calculated?*
    Regards,
    Ranjan

    Dear,
    Quota arrangement divides the total requirements generated over a period of time among the sources of supplied by assigning a quota.
    Quota u2013 quota is equal to a number and its represents the proportionate of requirements. Total quota of all the vendors is equal to 100% of requirements.
    This quota arrangement is also specific to material and plant level.
    Quota rating = quota allocate quantity + quota base quantity / quota.
    Quota based quantity used only when a new vendor introduced.
    In the as on date situation, the minimum quota ratings will considered as preferred vendor.
    The 2 vendors has 2 same quota rating, the vendor who is having the highest quota will considered first.
    In the running quota, a new source of supply is included. (In situation of short supply) including a new source not means to reduce the quota for existing.
    Regards,
    Syed Hussain.

  • How to calculate the average inventory in ABAP

    Dear All,
    Please find the below formula and this formula how to calculate the Average inventory at value.Please let me know the abap base tables and the corresponding fields.
    Formula
    Inventory Turnover = Cost of Goods Sold (COGS) / Average Inventory at value.
    Thanks
    Regards,
    Sai

    Hi Arivazhagan,
    Thanks for your quick response .
    The field MBEWH from the table is fulfill the average inventory at value.
    For Eg :I want to calculate Inventory Turnover = Cost of Goods Sold (COGS)/
    Average Inventory at value.
    so shall i take Inventory Turnover = Cost of Goods Sold (COGS)/MBEWH
    The above formula will meet my requirement to find the average inventory Turnover.
    Thanks
    Regards,
    Sai

Maybe you are looking for

  • Can't move emails from one account to another in native Mail app IOS 7.0.3

    I can select an email and click on move, and MAIL looks like working (the animation seems to move the email to a different account folder) but indeed is not moving the email! This was working with the previous release of IOS Mail works if I try to mo

  • Error 11222 when trying to connect to iTunes store

    When I am on my HP laptop, iTunes will not let me load the store and it says Error 11222. My 5th gen iPod Nano still connects and works prefectly fine on iTunes. I have tried to fix it on my own by using Apple support, but nothing is working, and I h

  • Hpw do you change Calendar server's from email address for a user?

    I have a user account with multiple email aliases.  The Calendar server has picked up one of those as the from address when scheduling appointments.  Does anyone know how to change this address to force it to use the primary address? I looked into th

  • Pricing SRM

    Hi all,         I created a new condition type for pricing, but the first test the systems added the new line but without any data and only for display. I deleted and created again, but this time I had the following error: Does not matter if I Create

  • Mass upload for VK11 - Additional Data

    I'm currently using the RV_CONDITION_COPY to upload data to VK11.  However, I can't find a way to include in the upload the Additional Data (F7).  I will be including only the field "Max.number.of.orders (ANZAUF). I will appreciate your help guys.