Summerize accoding to all leves in herarchial table

I have an herarchial table that contain the folowing structure and data:
STRUCTURE_ID NUMBER(5) not null;
CATALOG_ID NUMBER(5) not null;
MASTER_ITEM_ID VARCHAR2(15) not null;
DETAILED_ITEM_ID VARCHAR2(15) not null;
4     3     0     1
4     3     1     35
4     3     35     35000
4     3     35     35010
4     3     0     3
4     3     3     50
4     3     50     50000
4     3     3     51
4     3     51     51000
4     3     51     51200
In different table, I have financial data. Structure and data:
STRUCTURE_ID NUMBER(5) not null;
CATALOG_ID NUMBER(5) not null;
ITEM_ID VARCHAR2(15) not null;
SUM NUMBER(14,4);
1     4     3     35000     -3000000
2     4     3     50000     -56886.25
3     4     3     51000     61600
4     4     3     51200     10800
I want to have a select statement, that use use the connect by prior statement (to gain the herarchial structure), and to use a group by rollup to have totals to all the hirarchial levels.
I tried the next sql statement, but it not summerise all the levels in the herarchy:
select b.master_item_id, a.item_id, sum(a.sum)
from fin_data a, fin_structure b
where b.detailed_item_id =a.item_id (+)
and a.structure_id=4
and a.structure_id=b.structure_id
connect by prior b.detailed_item_id=b.master_item_id
group by rollup (b.master_item_id,item_id);
The result looks like this :
35     35000     -3000000
35          -3000000
50     50000     -56886.25
50          -56886.25
51     51000     61600
51     51200     10800
51          72400
          -2984486.25
There are no totals to masters 1 and 3.
What I do wrong?

Here is my improved solution:
- Instead of a scalar subquery, the SUMS factored subquery calculates all the sums at once, then I join to it.
This technique performs better if the result set is large.
- The TOTALS factored subquery "adds" the additional lines so you don't have to insert them.
- I filter out the detail line that has no SUM because the OP did.
Here is a test with two structure/catalog combinations:insert into fin_structure select 3,2,master_item_id, detailed_item_id from fin_structure;
insert into fin_data select 3,2,item_id, fin_sum from fin_data;
WITH sums AS (
  SELECT S.STRUCTURE_ID, S.CATALOG_ID, S.MASTER_ITEM_ID ITEM_ID,
  sum(CONNECT_BY_ROOT(FIN_SUM)) FIN_SUM_SUM
  from FIN_STRUCTURE s
  LEFT JOIN FIN_DATA d
    ON (s.STRUCTURE_ID, s.CATALOG_ID, s.DETAILED_ITEM_ID)
    = ((D.STRUCTURE_ID, D.CATALOG_ID, D.ITEM_ID))
  START WITH d.fin_sum is not null
  CONNECT BY (S.STRUCTURE_ID, S.CATALOG_ID, S.DETAILED_ITEM_ID)
    = ((PRIOR S.STRUCTURE_ID, PRIOR S.CATALOG_ID, PRIOR S.MASTER_ITEM_ID))
  group by s.structure_id, s.catalog_id, s.master_item_id
  UNION ALL
  SELECT STRUCTURE_ID, CATALOG_ID, ITEM_ID, FIN_SUM FROM FIN_DATA
), TOTALS AS (
  SELECT DISTINCT STRUCTURE_ID, CATALOG_ID,
  'Total' MASTER_ITEM_ID, MASTER_ITEM_ID DETAILED_ITEM_ID
  FROM FIN_STRUCTURE
  where master_item_id not in (select detailed_item_id from fin_structure)
SELECT A.*, b.fin_sum_sum
FROM (
  SELECT * FROM TOTALS
  UNION ALL
  SELECT * FROM FIN_STRUCTURE
) a
left join sums b
      ON (a.STRUCTURE_ID, a.CATALOG_ID, a.DETAILED_ITEM_ID)
      = ((B.STRUCTURE_ID, B.CATALOG_ID, B.ITEM_ID))
WHERE B.FIN_SUM_SUM IS NOT NULL
START WITH A.MASTER_ITEM_ID = 'Total'
CONNECT BY (A.STRUCTURE_ID, A.CATALOG_ID, A.MASTER_ITEM_ID)
  = ((PRIOR A.STRUCTURE_ID, PRIOR A.CATALOG_ID, PRIOR A.DETAILED_ITEM_ID));
STRUCTURE_ID CATALOG_ID MASTER_ITEM_ID  DETAILED_ITEM_ID FIN_SUM_SUM
           3          2 Total           0                -2984486.25
           3          2 0               1                   -3000000
           3          2 1               35                  -3000000
           3          2 35              35000               -3000000
           3          2 0               3                   15513.75
           3          2 3               50                 -56886.25
           3          2 50              50000              -56886.25
           3          2 3               51                     72400
           3          2 51              51000                  61600
           3          2 51              51200                  10800
           4          3 Total           0                -2984486.25
           4          3 0               1                   -3000000
           4          3 1               35                  -3000000
           4          3 35              35000               -3000000
           4          3 0               3                   15513.75
           4          3 3               50                 -56886.25
           4          3 50              50000              -56886.25
           4          3 3               51                     72400
           4          3 51              51000                  61600
           4          3 51              51200                  10800Best regards, Stew

Similar Messages

  • Can VC print all data in a table?

    Hello!
      Can VC print all data in a table?
    Best Reagrds!
    csquare.

    I write it in this post:
    Re: Qusetion about Table View.
    It is possible, that you have to use the paging options.
    Regards,
    Marcel

  • Incomplete Data on report (report does not show all records from the table)

    Hello,
    I have problem with CR XI, I'm running the same report on the same data with simple select all records from the table (no sorting, no grouping, no filters)
    Sometimes report shows me all records sometimes not. Mostly not all records on the report. When report incomplete sometimes it shows different number of records.
    I'm using CR XI runtime on Windows Server 2003
    Any help appreciated
    Thanks!

    Sorry Alexander. I missed the last line where you clearly say it is runtime.
    A few more questions:
    - Which CR SDK are you using? The Report Designer Component or the CR assemblies for .NET?
    - What is the exact version of CR you are using (from help | about)
    - What CR Service Pack are you on?
    And a troubleshooting suggestion:
    Since this works on some machines, it will be a good idea to compare all the runtime (both CR and non CR) being loaded on a working and non working machines.
    Download the modules utility from here:
    https://smpdl.sap-ag.de/~sapidp/012002523100006252802008E/modules.zip
    and follow the steps as described in this thread:
    https://forums.sdn.sap.com/click.jspa?searchID=18424085&messageID=6186767
    The download also includes instructions on how to use modules.
    Ludek

  • Get all rows from a table control

    Hi All,
    I have a table control with one column. What function should I use to retrieve all the rows ? Do I need to iterate row by row and read each row or is it possible to do it in one function ?
    Thanks,
    Kanu
    Solved!
    Go to Solution.

    Supposing vells in the column have all the same data type, you can retrieve the whole column with a single instruction:
    GetTableCellRangeVals (panel, control, VAL_TABLE_COLUMN_RANGE (1), array, VAL_COLUMN_MAJOR);
    The array passed must be large enough to retrieve all data. Alternatively, you may substitute the macro VAL_TABLE_COLUMN_RANGE with the appropriate MakeRect instruction.In case your table was dinamically built, you can obtain the nu,ìmber of rows using GetNumTableRows and dimension your array accordingly.
    The above macro is defined in userint.h together with some other useful macros that can be used to access data in a table.
    There are some precautions to take in case of string values or some cell type (ring, combo box, button...) that are described in the hell for the function.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • Is there a way to match all columns in a table?

    Oracle 11.1.0.7:
    Is there any way to say match all the columns of Table A with Table B? For eg:
    Table A
    id
    name
    Table B
    id
    name
    Is there a way to say match all the columns with similar column in other in a select instead of doing "where A.id = B.id and A.name = b.name"? I am trying to write a compare script to test that replication is indeed doing the right thing.

    user628400 wrote:
    If 2 columns have null values does it match that too? I'll run some tests.It does
    SQL> ed
    Wrote file afiedt.buf
      1  with a as (select 1 col1, null col2 from dual
      2             union all
      3             select 2 col1, null col2 from dual
      4             union all
      5             select null, null from dual),
      6       b as (select 1, null from dual
      7             union all
      8             select null, null from dual)
      9  select *
    10    from a
    11  minus
    12  select *
    13*   from b
    SQL> /
          COL1 C
             2
    While I was doing the comparision I found that if column x in Table A has "null" and same column x in table B has "null" and if I do "where A.x = B.x" then it doesn't return the row. I am not sure why. Does "minus" work the same way?That's because NULL will never equal any value (including NULL) and NULL will never not equal any value (including NULL). That is
    NULL = 1 is unknown (which maps to false)
    NULL = NULL is unknown
    NULL != 1 is unknown
    NULL != NULL is unknown
    If you want to compare columns that may have NULL values, you need to use IS NULL and IS NOT NULL or use the NVL function to map NULL values to a non-NULL but impossible value, i.e.
    WHERE (a.x = b.x OR
            (a.x is null and b.x is null))or
    WHERE nvl(a.x,-17) = nvl(b.x,-17)assuming that -17 is not a valid value for either a.x or b.x
    Justin

  • How to see all the list of tables in APEX

    Hi
    COuld anyone of you help me find out how to see all the tables present in APEX ?
    I can see all the list of tables when i give the command : select * from tab
    But apart from finding like this , is there any way to find out all the list of tables ?
    Regards,
    Tasha

    Hi Tasha,
    For Apex 3.2 ;
    select * from dba_tables where owner in
    (select workspace from APEX_030200.APEX_WORKSPACES)
    tab is a synonym for user_tables
    http://www.oreillynet.com/pub/a/network/2002/10/28/data_dictionary.html
    hope this helps you
    regards
    mozkavra

  • BPM question - Delete all entries in a table and Insert data

    Hello,
    I'm new to PI BPM and have this scenario to implement.
    I'm supposed to delete all entries in a table and then insert data to the same table.
    Is BPM necessary? If it is, what is the best practice to implement my scenario?
    I want to make sure that all entries were successfully deleted before insert data.
    How do I check if all entries in the table were deleted successfully before
    initiating insert.
    Thank you.
    -Won

    Hi Won,
    Yes, this is possible without BPM.
    You have to create 2 separate mappings, each for deleting & inserting the records.
    You will have 2 different Interface mappings as well.
    But in ID, create a single interface determination.
    In that, configure Deletion interface mapping first and in the second row, configure Insertion interface mapping.
    Please make sure that the parameter 'Mainain Order At runtime' is checked in interface determination.
    -Supriya.

  • Delete all record in a table, the insert speed is not change.

    I have an empty table, and i insert a record need 100ms,
    when this table has 40,0000 record, i insert a record need 1s, this is ok, because i need do a compare based an index before insert a record, so more record, need more time.
    The problem is when i delete all record in this table, the insert time is still 1s, not reduce to 100ms.Why?

    Hello,
    Read through this portion of oracle documentation
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/logical.htm#CNCPT004
    The reason is still taking 1s, because when you inserted 400K record because HWM (The high water mark is the boundary between used and unused space in a segment.) moved up. And when you deleted all the records your HWM still at the same marker and didn't get to reset to 0. So when you insert 1 record it lookings for free space and after finding (generally a regular inserts got 6 steps it inserts data). If you truncate your table you and try it again it will be faster as your HWM is reset to 0.
    Regards

  • SQL query problem:  how tp select all duplication in the table?

    Hi all,
    I have a table with the following columns:
    1. contact_id
    2. fname
    3. lname
    4. email
    The email column must be unique (unfortunately it's not); therefore, I have to delete all duplication in the system.
    Question: how can I select all duplication in the table?
    I tried this...didn't work (I'm expecting only the emails that are duplicated)
    THANK YOU!
    SELECT distinct TC1.email, TC1.contact_ID FROM ta_contacts AS TC1 where TC1.email <>'' AND TC1.email not in (   SELECT distinct TC2.email   FROM ta_contacts AS TC2   where TC2.email <>'' )

    Sounds like an SQL and not a JDBC question. In any case, if a column needs to be unique, then you should, of course, designate a unique constraint on the table so you never end up with duplicates in the first place.

  • I am unabel to select all rows in advance table using Select All option

    Hi all ,
    I am unable to select all the rows in advance table . Select All option in advance table selects all rows present in working page but doesnt select the rows which we get by clicking next button ... i am having a requirement to select all the rows with a single click on select all , is it possible ..
    Plz help
    Thanking you all

    Hi,
    I'm using an adf table but i don't want to have the "Select All" and "Select None" options above the table. Does someone know how i have to remove those links?
    Or is there indeed a solution to override the methodes of those links?
    Message was edited by:
    Alexander

  • Delete all rows in a table

    I have read two articles how to use sql adapter with delete.
    http://btsguru.blogspot.se/2011/10/wcf-sql-adapter-table-operations.html
    http://social.technet.microsoft.com/wiki/contents/articles/29146.biztalk-server-2013-crud-operation-with-wcf-sql-adapter-and-correlation.aspx?wa=wsignin1.0
    Is it a way to delete all rows in a table?
    I have tried to send <ns0:Rows>*</ns0:Rows> with no luck.
    Challan

    I'm not expert in Biztalk but one of the options to call stored procedure that contains the delete script:
    http://geekswithblogs.net/StuartBrierley/archive/2011/10/19/biztalk-server-2010---using-the-wcf-sql-adapter-to-make.aspx
    Sql Delete all rows from table Script:
    DELETE FROM table_name;
    or 
    TRUNCATE TABLE mytable;
    Trucnate vs Delete:
    http://www.mssqltips.com/sqlservertip/1080/deleting-data-in-sql-server-with-truncate-vs-delete-commands/
    Fouad Roumieh

  • Select All rows  in WebDybpro Tables

    Hi All ,
    I am working on NWCE 7.1 . i have table in my UI. and want to give slect all rows functionality in table. in NWCE is there
    any way to give select all rows in table with out using , button and writing action to iterate node bound to table.
    also i want to know about RowCreator ui element for table.(when we right click on table we get insert -> rowcreator). How this works
    Thanks in advance
    Regards
    Kavita

    Hi Kavita,
    The row creator Ui element is not visible in table itself you can see it only in outline window.
    and in its on create event you have to write the code which i have provided above. it will not be visible in table at design time but at run time when you will click on the last rows lead selection it will call the associated event and one row will be added to the table.
    Regards
    Jeetendra

  • Select all rows in af:table

    Hi,
    I need to select all rows in af:table, not only on current page.
    I have following code on custom select all action:
    getTable2().getSelectionState().addAll()and on button that needs to perform some action on selected rows I have this code
            Set selectedRowSet = getTable2().getSelectionState().getKeySet();
            if (!selectedRowSet.isEmpty()) {
                Iterator it = selectedRowSet.iterator();
                while(it.hasNext()) {
                    Key key = (Key)it.next();
           }selectedRowSet.iterator() method throws java.lang.ClassCastException: oracle.jbo.Key.
    When I select rows with af:tableSelectMany, which is inside selection facet and selects only rows on current page, this works fine.
    Any ideas?
    Thanks

    Hi,
    you can only access those data that is queried and part of the CollectionModel to the time you select all
       RichTable _table = employeesBackingBean.getEmployeeTable1();
       RowKeySet rks = new RowKeySetImpl();
      CollectionModel model = (CollectionModel)_table.getValue();
      int rowCount = model.getRowCount();
          for (int i = 0; i < rowCount; i++) {
               model.setRowIndex(i);
               //note that in the simple POJO case, the row key is the same
               //as the index. However, it would be wrong to just rely on this
               //because other models or custom table models my return a more
               //comples key. Therefore we iterate over the available rows to
               //obtain the keys.
               Object key = model.getRowKey();
               //add the row keys to the RowKeySet to mark selected
               rks.add(key);
       _table.setSelectedRowKeys(rks);
       AdfFacesContext.getCurrentInstance().addPartialTarget(_table);Frank
    Frank

  • For All Entries with two tables

    Hi All,
             Can we use FOR ALL ENTRIES with two tables. for example
    SELECT * FROM MKPF INTO TABLE T_MKPF
             WHERE BUDAT IN S_BUDAT.
    SELECT * FROM MARA INTO TABLE T_MARA
             WHERE MTART IN S_MTART AND
                            MAKTL IN S_MAKTL.
    SELECT * FROM MSEG INTO TABLE T_MSEG
           FOR ALL ENTRIES IN  "T_MKPF AND T_MARA"
                  WHERE MBLNR EQ T_MKPF-MBLNR AND
                                 MATNR EQ T_MARA-MATNR.
    can we do it like this or any other way to do this plz tell. I waitting for your responce.
    Thanks
    Jitendra

    Hi,
    u cannot do like this....chek some documentation on it..
    1. duplicate rows are automatically removed
    2. if the itab used in the clause is empty , all the rows in the source table will be selected .
    3. performance degradation when using the clause on big tables.
    Say for example you have the following abap code:
    Select * from mara
    For all entries in itab
    Where matnr = itab-matnr.
    If the actual source of the material list (represented here by itab) is actually another database table, like:
    select matnr from mseg
    into corresponding fields of table itab
    where ….
    Then you could have used one sql statement that joins both tables.
    Select t1.*
    From mara t1, mseg t2
    Where t1.matnr = t2.matnr
    And T2…..
    So what are the drawbacks of using the "for all entires" instead of a join ?
    At run time , in order to fulfill the "for all entries " request, the abap engine will generate several sql statements (for detailed information on this refer to note 48230). Regardless of which method the engine uses (union all, "or" or "in" predicates) If the itab is bigger then a few records, the abap engine will break the itab into parts, and rerun an sql statement several times in a loop. This rerun of the same sql statement , each time with different host values, is a source of resource waste because it may lead to re-reading of data pages.
    returing to the above example , lets say that our itab contains 500 records and that the abap engine will be forced to run the following sql statement 50 times with a list of 10 values each time.
    Select * from mara
    Where matnr in ( ...)
    Db2 will be able to perform this sql statement cheaply all 50 times, using one of sap standard indexes that contain the matnr column. But in actuality, if you consider the wider picture (all 50 executions of the statement), you will see that some of the data pages, especially the root and middle-tire index pages have been re-read each execution.
    Even though db2 has mechanisms like buffer pools and sequential detection to try to minimize the i/o cost of such cases, those mechanisms can only minimize the actual i/o operations , not the cpu cost of re-reading them once they are in memory. Had you coded the join, db2 would have known that you actually need 500 rows from mara, it would have been able to use other access methods, and potentially consume less getpages i/o and cpu.
    In other words , when you use the "for all entries " clause instead of coding a join , you are depriving the database of important information needed to select the best access path for your application. Moreover, you are depriving your DBA of the same vital information. When the DBA monitors & tunes the system, he (or she) is less likely to recognize this kind of resource waste. The DBA will see a simple statement that uses an index , he is less likely to realize that this statement is executed in a loop unnecessarily.
    Beore using the "for all entries" clause and to evaluate the use of database views as a means to:
    a. simplify sql
    b. simplify abap code
    c. get around open sql limitations.
    check the links
    http://www.thespot4sap.com/articles/SAPABAPPerformanceTuning_ForAllEntries.asp
    The specified item was not found.
    Regards,
    Nagaraj

  • Select all fields of a table while designing the layout of a query

    Can some one tell me how to select all fields of a table at a single stroke while designing the layout of a query....this would greatly reduce the time spent to select each and every field of a table into the layout....
    Thanks in advance

    Hi,
    Declare your internal table (Say it is itab).
    Then :-
    Select * from <tablename>.
    Eg,.Select * from mara.
    Then display it using:-
    Loop at itab.
    write:/ wa_itab.   "Workarea.
    endloop.
    Thanks and regards,
    Prerna

Maybe you are looking for