Query to total columns

Hello,
Using Oracle 11g R2. I'm trying to present data in the following format. I've come up with the query below, but I don't think it's right.
COL1
COL2
COL3
NVL(COL4,SUM(COL4))
NVL(COL5,SUM(COL5))
NVL(COL4-COL5,SUM(COL4-COL5))
1
10
10/29/2013 12:04:02
40
50
-10
1
20
10/30/2013 12:04:21
80
20
60
1
30
10/31/2013 12:04:38
25
60
-35
total 1
145
130
15
2
40
11/01/2013 12:05:03
90
10
80
2
50
11/02/2013 12:05:41
85
50
35
2
175
60
115
     total 2
320
190
130
grant total
465
320
145
create table testrollupRh (col1 number, col2 number, col3 date, col4 number, col5 number)
insert into testrolluprh values (1, 10, sysdate - 10, 40, 50)
insert into testrolluprh values (1, 20, sysdate - 9, 80, 20)
insert into testrolluprh values (1, 30, sysdate - 8, 25, 60)
insert into testrolluprh values (2, 40, sysdate - 7, 90, 10)
insert into testrolluprh values (2, 50, sysdate - 6, 85, 50)
select col1,
         col2,
         col3,
         NVL(col4, SUM(col4)),
         NVL(col5, SUM(col5)),
         NVL(col4 - col5, SUM(col4 - col5))
from  testrolluprh
where col1 is not null
and col2 is not null
and col3 is not null
and col4 is not null
and col5 is not null
group by rollup ((col1), (col1, col2, col3, col4, col5))
Any help is appreciated.

Hi,
I don't see any problems with your code.
You should have an ORDER BY clause, of course.  Almost all queries need an ORDER BY clause, but that's often omitted in this forum.
If you want to put labels, such as 'Total 1' and 'Grand Total' on the super-aggregate rows, you can use CASE and GROUPING, like this:
SELECT    CASE
              WHEN  GROUPING (col2) = 0  THEN  TO_CHAR (col1)
              WHEN  GROUPING (col1) = 0  THEN  '   Total ' || col1
                                         ELSE  '     Grand Total'
          END                                    AS out1,
          col2,
          col3,
          NVL (col4,        SUM (col4))          AS out4,
          NVL (col5,        SUM (col5))          AS out5,
          NVL (col4 - col5, SUM (col4 - col5))   AS out6
FROM      testrolluprh
WHERE     col1   IS NOT NULL
AND       col2   IS NOT NULL
AND       col3   IS NOT NULL
AND       col4   IS NOT NULL
AND       col5   IS NOT NULL
GROUP BY  ROLLUP ( (col1)
                 , (col1, col2, col3, col4, col5)
ORDER BY  col1, col3
Output:
OUT1                   COL2 COL3              OUT4       OUT5       OUT6
1                        10 29-Oct-2013         40         50        -10
1                        20 30-Oct-2013         80         20         60
1                        30 31-Oct-2013         25         60        -35
   Total 1                                     145        130         15
2                        40 01-Nov-2013         90         10         80
2                        50 02-Nov-2013         85         50         35
   Total 2                                     175         60        115
     Grand Total                               320        190        130

Similar Messages

  • Query Print Layout column sum in report footer

    Hi Experts,
    I am utilizing a query and created the layout via Query Print Layout. I am using sort and group functions which filters my column sums for a certain field conditions. My problem is that when I want to make a total column sum for all the repetitive footers' column sum, the system shows a message that only repetitive areas can be average/ sum.
    Is this the standard behaviour of the system? Or is this a limitation if we are using the print layout via Query print layout. Any workaround to get the final total for all pages column sum?
    Please advise.
    Thanks,
    Don

    hello
    Don
    what u want actually i dint understood as i now but u need colsum of ur repetitive fields or repetitive footer fields
    I).if u want repetitive footer all columns sum take the contact of those fields ex:(concat(F_a+F_B -
    )) so u can get ur total for repetitive footer fields
    II).if u want repetitive area lines colsum for every field u take as colsum with respect to the unique id of repetitive area fields
    regards
    Jenny

  • Bex 7 - Sort Total Column

    Hi
    I have built a query with 2 characteristics drilled vertically and another 1 characteristic drilled horizontally.  I have a single key figure in the report and there is an 'Overall Result' columns at the far right of the report.  Great.
    The report is sorting by the first column but I want to be able to sort everything by the total column at the far right of the report.
    I've tried opening the properties on my key figure in Analyzer but the sorting tab is greyed out.
    Can anyone help with this?
    Thanks

    Just incase anyone finds this and has the same problem, I resolved the issue by first removing the drill-across value, sorted the key figure (right click) and then added back the drill-across value.
    With the drill-across figure, I did not have access to the sort menuu

  • Formatting total columns in an OAAdvancedTableBean

    Hi All,
    I need to be able to format the table total columns in the format:
    Formatter formatter = new OADecimalValidater("###,###,###,##0;-###,###,###,##0", "###,###,###,##0;-###,###,###,##0");
    (no decimal places)
    I've tried various things e.g.
    DictionaryData columnFormat = (DictionaryData)columnBean.getColumnFormat();
    if (columnFormat == null) {
    columnFormat = new DictionaryData();
    Formatter formatter = new OADecimalValidater("###,###,###,##0;-###,###,###,##0", "###,###,###,##0;-###,###,###,##0");
    columnFormat.put(COLUMN_FORMAT_ATTR,formatter);
    columnFormat.put(ON_SUBMIT_VALIDATER_ATTR,formatter);
    columnBean.setColumnFormat(columnFormat);
    ... but nothing works.
    I've searched the forum for previous responses to this query and the only solution put forward appears to be columnBean.setAttributeValue(CURRENCY_CODE,"USD");
    Which is no use to me because of the decimal places presented.
    Is there another way?
    Thanks,
    John

    To bring the latest 12 in to the report or input schedule do the following, Have a property in the entity dimension as CURMONTH, and put it in the excel sheet using EVPRO. Introduce 12 columns to the right of the excel sheet. Using the evtim function subtract the month respectively from the current month. This will put the latest 12 months in all the cells. Now in your colkeyrange make the change so that it picks up's the 12 month that you have laid out and bring the data as part of your refresh or expand. Now hide the columns. In the report you will have column called latest 12. Create a excel formula to sum up the all the months(latest 12). This way you can get the latest 12.
    Other way of getting the latest 12 is , create custom measure for latest 12 months and then simply include in your report.
    I hope this helps.
    thanks
    david

  • Query rows to column

    dear Friends,
    i need query row to column
    example
    1 a data1 data2 data3
    2 b data1 data2 data3
    output format
    1 2 a
    data1 data2 data3

    Please consider the following when you post a question.
    1. New features keep coming in every oracle version so please provide Your Oracle DB Version to get the best possible answer.
    You can use the following query and do a copy past of the output.
    select * from v$version 2. This forum has a very good Search Feature. Please use that before posting your question. Because for most of the questions
    that are asked the answer is already there.
    3. We dont know your DB structure or How your Data is. So you need to let us know. The best way would be to give some sample data like this.
    I have the following table called sales
    with sales
    as
          select 1 sales_id, 1 prod_id, 1001 inv_num, 120 qty from dual
          union all
          select 2 sales_id, 1 prod_id, 1002 inv_num, 25 qty from dual
    select *
      from sales 4. Rather than telling what you want in words its more easier when you give your expected output.
    For example in the above sales table, I want to know the total quantity and number of invoice for each product.
    The output should look like this
    Prod_id   sum_qty   count_inv
    1         145       2 5. When ever you get an error message post the entire error message. With the Error Number, The message and the Line number.
    6. Next thing is a very important thing to remember. Please post only well formatted code. Unformatted code is very hard to read.
    Your code format gets lost when you post it in the Oracle Forum. So in order to preserve it you need to
    use the {noformat}{noformat} tags.
    The usage of the tag is like this.
    <place your code here>\
    7. If you are posting a *Performance Related Question*. Please read
       {thread:id=501834} and {thread:id=863295}.
       Following those guide will be very helpful.
    8. Please keep in mind that this is a public forum. Here No question is URGENT.
       So use of words like *URGENT* or *ASAP* (As Soon As Possible) are considered to be rude.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Sorting on a total column or calculated column in a pivot table

    We have a pivot table showing customer activity by month. We have added a calculated field to show the YTD average instead of a total column. Is there a way to sort on this calculated field? We have applied a sort on the measure in the criteria, and our resulting pivot table sorts by the values in the most recent month, not by the YTD average.

    I think we cant sort when we use a pivot view becoz all the rows are already fixed. Say your rows are sales and volume and columns are year 2007 and 2008, imagine if you r given the sorting ability then if number of units solds is more then it need to change the rows (but in pivot table rows are fixed). So, we cant sort in pivot tables!!

  • How to write a case statement for the totals column of two different years (2013 and 2014) of the same month so that I can get a +/- column

    Please Help!!!
    How to write a case statement for the totals column of two different years (2013 and 2014) of the same month so that I can get a +/- column.
                                      January 2014         January
    2013                            +/-
                    Region   Entry   Exit  Total    Entry   Exit   Total   (Total of Jan2014-Total of Jan2013)
                    A               2         3      
    40        5       7        30                    40-30= 10

    What is a table structure? Sorry cannot test it right now..
    SELECT <columns>,(SELECT Total FROM tbl WHERE Y=2014)-(SELECT Total FROM tbl WHERE Y=2013)
    FROM tbl
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • How to change the label of a total column in pivot view

    Hi
    Is there a way to change the label of the column created by analytics when calculated row based totals? Currently, it is same as the measure label to be summed.
    Thanks

    Hi
    Thanks for your help, actually that option works for the total displayed as the latest row, I mean the label for the total row. However, for the total column, the name stays as the same as the measure label.
    Can you further help me on the issue?
    Thanks

  • Query on database column not working

    Hi
    i have a block which has around 40 columns.the block is based on a table which has more than 20000 records.When i try 2 make query on a Database column in Enter-Query mode its not working.It fetching all the records instead of the respective records
    THe block property is said to yes
    i have even set the query allowed,only propery of the column to YEs
    Can any one help me out of this??? do i need to set any other property???

    Thanks a lot for james and THomas and to all. The error is that i have missed the bracket for OR in my where condition ...Now im able to query all the columns.
    Thanks very much...
    I have one more doubt .i have a requirement in which when the user goes to the child block and makes a query is it possible to bring the associates parent records..wht code or trigger we need to rite ?
    normally we do for parent block when we query we populate the child. is this can be done?

  • Total column not changed between header & detailed screen

    I have a report with header data contains contracts information and detailed data contains orders, debit credit memo related to the contracts.  I write the report using CL_SALV_TABLE to display  contract value, usage  in full ALV_GRID. When I double click on a contract line, it will go to the second screen to show all orders related to that contract.  The second screen with orders information will be in a screen on a container with all standard function ( Export list to Excel, Word format etc..), sort, total and sub total) and a push button to go back to first screen.
      The report works fine but when I am on the second screen, I total and sub total the amount on that screen in order to check with total value in header record and it total up correctly by currency.
    But when I go back to the first screen, select a different contract record,  double click on the line to get to the order info screen,  all order data display correctly except that the total column still show amount from the previous detailed screen.  If I select the column again and click the total, it will total correctly.   I did refresh the data in the screen before go back to first screen but it does not solve the problem.
    DOES ANYONE HAVE A SIMILAR SYMPTON LIKE THIS? 
    Thanks for your advice.
    On the order detailed screen, I create a container with screen 100 and write PAI, PBO like this:
    Screen 100:
    process before output.
      module status_0100.
      module load_data_to_control.   "Load usage data to screen
    process after input.
    module user_command_0100.
    module status_0100 output.
      set pf-status 'PF100'.
      set titlebar 'T100'.
    endmodule.  
    module load_data_to_control output.
      if gr_cont is not bound.
        create object gr_cont
          exporting
            container_name = 'CONTAINER_100'
          exceptions
            others         = 1.
        if  sy-subrc <> 0.
          message a052(zvn) with
          ' No container object found'.
        endif.
        try.
            cl_salv_table=>factory(
            exporting
         list_display   = list_display
              r_container = gr_cont
          container_name =
            importing
              r_salv_table = gr_alv_2
            changing
              t_table = gt_orders
          catch cx_salv_msg into gr_error.
        endtry.
        perform set_functions.        " Set all standard function in screen 100
        perform set_column_order using gr_alv_2.       "Set column format, display
        perform handle_events.                       "Register events for selection mode & user commands
         Display order screen 0100
        gr_alv_2->display( ).
      else.
        gr_alv_2->refresh( ).
    endif.
    module user_command_0100 input.
      data: wa_d type gt_dtab,
            ls_contract type vbeln,
            ls_order    type vbeln,
            ls_invoice  type vbeln,
            ls_belnr type belnr_d,
            ls_row   type char10.
      data: l_text type char128.
        case  sy-ucomm.
        when 'RETURN' or 'BACK' or 'EXIT' or 'CANCEL'.
    *... refresh the table in order to see the new data
          gr_alv_2->refresh( ).
          leave to screen 0.
        when others.
      endcase.
    endmodule.                 " USER_COMMAND_0100  INPUT

    Hi ,
    When you are using the CL_SALV_TABLE, what is the use of screen 100. Better populate the evnts table in CL_SALV_TABLE
    Cheers
    Pavan

  • Problem in Formatting Total Column in Classic Table

    Hi,
    I am using Classic Table.
    I am able to format the column data but i am unable to format the column total value. I searched in forum and tryed something like in the following thread..
    Problem in formatting Total Value in advanced table
    since i am using Classic Tables i am unable to do it.
    I also tried the following
    OATableBean table = ....
    OATotalRowBean totalRowBean = (OATotalRowBean)table.getColumnFooter();
    System.out.println("Formattotal : "+totalRowBean);
    if (totalRowBean != null)
    oracle.cabo.ui.validate.Formatter formatter = new OADecimalValidater("#,##0.00;#,##0.00","#,##0.00;#,##0.00");
    totalRowBean.setAttributeValue(ON_SUBMIT_VALIDATER_ATTR, formatter);
    in this case the total is not formatted.
    I also refered developer guide. There i found how to set value to total column.. but how to get the value its not given..
    Any body pls help in this regards
    Thanks & Regards

    Hi,
    When i am using Classic Table i am able to format column data but i am not able to format the column Total
    Code i using ;
    OATableBean table = (OATableBean) webBean.findChildRecursive("EmpTestVO12");
    table.prepareForRendering(pageContext);
    OAMessageStyledTextBean totalBean = (OAMessageStyledTextBean)table.findChildRecursive("Salary");
    if(totalBean!=null)
    Formatter formatter = new OADecimalValidater("#,##0.00;(#,##0.00)","#,##0.00;(#,##0.00)");
    totalBean.setAttributeValue(ON_SUBMIT_VALIDATER_ATTR, formatter);
    //Properly formatting column data
    Whe i am using OAAdvance Table bean i am able to format the total but i am unabele to format the Column data. Using following code..
    OAAdvancedTableBean tableBean=(OAAdvancedTableBean)webBean.findChildRecursive("region2");
    tableBean.prepareForRendering(pageContext);
    OAColumnBean c1=(OAColumnBean)webBean.findChildRecursive("column12");
    if(c1!=null)
    c1.setAttributeValue(CURRENCY_CODE,"USD");
    Can anybody pls tell me how can i achieve both (column values and totals formatting) using either Classic Table or Advance Table..
    Thanks & Regards,
    Ram

  • Invoice Forms created with Adobe X - default value for "total" column should be blank

    I am new to the forum and just finding my way around.  Thank you for any help.
    I've created an invoice template in Word, then imported it into Adobe X Pro to turn it into a form.  Pretty simple, but one thing is troubling me.  I have the usual description, quantity, price each, and total columns.  The "total" columns are defaulting with a "0" in them, and I'd prefer they default blank unless an entry is made on that line.  I have right-clicked the "total" fields properties and the default values are blank already, but a zero still shows up.  I  know it can be done because one of my earlier forms defaults to a blank space until I entered something on that line. Then it calculated.  How can I make the "total" column blank unless something is entered there?
    Secondly, when I save a form and want to go back to make changes, I'm not seeing how to do this.  It seems to save as a .pdf, and if I want to modify the form portion I have to start all over from scratch.  What am I missing?
    Thanks so much for any help.

    George, thank you for the response.
    I've entered the script, and no change.  The "total" columns still show a (zero) 0 even when I have not entered anything in that line.
    For example:
    Description             Qty     Unit Price     Total
                                                                             0
                                                                             0
                                                                             0
    etc... all the day down the form.
    I'd like the zeroes not to appear when there is nothing on the Description lines.
    Incidentally, when selecting fields to use for calculations for the "total" column, we're supposed to place a check mark in the box of the fields to be calculated - and I can't CLICK in them.  I have to use the space bar.  Is that how it is supposed to work?
    Thanks for your quick response.
    J
    I

  • Result of an SQL query as a Column name of another query

    Hi Friends,
    Can we use a result of a SQL Query as a column name of another table to retrieve data. If so please help me.
    For eg :
    I have a table where is store numbers;
    select col1 from table1 where col1='5';
    and i have another table where .. this value of col is a column name..
    select ( select col1 from table1 where col1='5') from table2;
    Thanks in advance.

    Hi,
    ORAFLEX wrote:
    Hi Friends,
    Can we use a result of a SQL Query as a column name of another table to retrieve data. If so please help me.
    For eg :
    I have a table where is store numbers;
    select col1 from table1 where col1='5';
    and i have another table where .. this value of col is a column name..
    select ( select col1 from table1 where col1='5') from table2;
    Thanks in advance.Do you really mean that?
    select col1 from table1 where col1='5';That query will always return either '5' or nothing. Whatever you're trying to accomplish with that, you can do with an EXISTS query.
    Perhaps you meant to reference two different columns in that query:
    select col1 from table1 where col2='5';In that case, sorry, no, you can't do that without resorting to dynamic SQL.
    If the same column is used throughout the query (but could change every time you run the query), then the dynamic SQL might be pretty easy. In SQL*Plus, for example, you could use substitution variables, defined in another query at run-time.
    If there are only a few possible values that the sub-query could possibly return, and you know what they all are, then you can fake a dynamic query like this:
    SELECT     CASE     ( SELECT  col1
                FROM       table1
                WHERE       col2     = '5'
              WHEN  'BONUS'     THEN  bonus
              WHEN  'COMM'     THEN  comm
              WHEN  'SAL'     THEN  sal
         END     AS col1
    FROM     table2
    ;Sorry to give such a vague answer, but it's the best I can do with the information I have.
    It would help if you posted a little sample data (CREATE TABLE and INSERT statments for both tables), and the results you want to get from that data. If you want to pass a parameter to the query, give the results you want for a couple of different parameters.

  • Creating Query with dynamic columns to show results

    Hi experts,
    I need to know how to create a query with dynamic columns. Meaning, I don't want to create a query with fixed columns representing the 12 periods of the fiscal year to show me actuals as the fiscal year proceeds.
    For example, if I am currently in the middle of period 3 (March) of a fiscal year, when I execute the query, I need it to automatically only show me the 'Actuals' for periods 1 and 2, without seeing the columns from periods 3 to 12 showing blank.
    Then when I am in the middle period 5 (May) the query should ONLY show me the columns for periods 1 to 4 'Actuals', no results should be shown for periods 5 to 12 yet, and I don't want to even see blank columns for period 6 to 12.
    How do I define my columns, to achieve this.
    Maximum points will be awarded.
    Thanks Everyone.

    Hi Josh,
    I'm having a little difficuluty understanding what should be included in my restricted key figures.
    The time characteristics that I have available to use are:
    0FISCPER3 (posting period)
    0FISCYEAR (fiscal year), currently using SAP EXIT to default current fiscal year.
    0FISCVARNT (fiscal year variant).
    In addition, I have the following characteristics available to be used in the columns:
    Value type (10)
    version (currently I'm using variable for it)
    Currency type (020)
    Currency (USD).
    Can you explain what my restricted key figure should be based on and how it should look.
    I tried to create a restircted key figure using 0AMOUNT, and 0FISCPER3. For 0FISCPER3  I created a range from 1 to previous period (using SAP EXIT that supplied previous period).I also had value type, version, currency type, and currency included in that restricted key figure.Then when I tried to drag 0FISCPER3 under the restricted key figure once again, it wouldn't let me, probably because I've already used 0FISCPER3 in the restricted key figure.
    Please let me know if my explanation is not clear.
    Your step by step help would be great.
    Thanks
    Edited by: Ehab Mansour on Sep 23, 2008 2:40 PM

  • ABAP Query Quantity totals wrongly - M.Type 101+, 102-

    Hi Friends,
                    Could any one help me in this? I created an ABAP Query for the combination of T.codes MB51 & MB52. When I see the total qty for the item which has GR and Goods return document.........say first by 101 I rerceive 5 nos and using 122 returns 3 nos. Here Query shows total is 8nos instead of 2 nos.
                  The same way correnponding price also shows for 8nos instead 2 nos.
    Kind Regards,
    Sankar.K

    Hi Sankar,
    You can use MSEG-SHKZG field for +(plus)/-(minus) indicator where 'H' is -(minus) and 'S' is +(plus).
    I hope this can answer your problem.
    Regards
    Gunawan

Maybe you are looking for