Connect by query, need root element for each row

Hi,
I am working on a hierarchical query, using connect by prior. Each node in the tree has two properties, an type and a sequence.
The table that contains the hierarchy has 4 fields:
element_type_from
element_sequence_from
element_type_to
element_sequence_to
Each child has one parent, a parent can have multiple childeren. For a leaf, the fields element_type_to and element_sequence_to are null.
To generate a tree you can run:
select element_type_to
,      element_sequence_to
from   element_hierarchy
start with element_type_from = [root_element_type]
       and element_sequence_from = [root_element_sequence]
connect by prior element_type_to = element_type_from
       and prior element_sequence_to = element_sequence_fromThat works fine... however... not only do I want the child elements, I would like to return the root element sequence for each child (in our table the root element type is always the same). There are multiple root elements and I want to create a list containing all trees and each node in the tree must have its root as well.
There is the option of using sys_connect_by_path. This returns the root, but also the entire path to the current child. Also it returns a varchar2, that needs to be substr-ed and to_number-ed to get to the sequence... not nice.
warning, extremely ugly (but functional) code:
select element_type_to
,      element_sequence_to
,      to_number(substr(sys_connect_by_path(element_sequence_from ,','),2,instr(sys_connect_by_path(element_sequence_from ,',') ||',',',',2)-2)) root_sequence
from   element_hierarchy
start with element_type_from = [root_element_type]
       and element_sequence_from = ( select [root_element_sequence] from all_root_elements )
connect by prior element_type_to = element_type_from
       and prior element_sequence_to = element_sequence_fromThere has to be a simple solution that I am missing here! Can you help?
Edit: Oops, database version is 10.2.0.4.0

Hi,
What you posted was actually the best way to do it in Oracle 9.
Since you're using Oracle 10, you can use CONNECT_BY_ROOT, as William said:
select element_type_to
,      element_sequence_to
,      connect_by_root element_sequence_from  AS root_sequence          -- Changed
from   element_hierarchy
start with element_type_from = [root_element_type]
       and element_sequence_from = ( select [root_element_sequence] from all_root_elements )
connect by prior element_type_to = element_type_from
       and prior element_sequence_to = element_sequence_from 
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables, and the results you want from that data.
Explain how you get those results from that data.

Similar Messages

  • Hyperion Business Rule Error: Cannot retrieve connected root element for

    Hi,
    I am trying to open a business rule in EAS and I am recieving the following error: Cannot retrieve connected root element for Planning/Servername/Application/Database.
    I am able to connect to the application via Planning Web, but I am recieving this message when I am trying to open the business rule in eas.
    hoping if someone has come across this issue before and has been able to successfully fix it.

    Hi,
    I have 2 BRs, one is with RTP and another without RTP (basically a clacscript as BR).
    I don't have any issue with the BR without RTP.
    When I try to validate BR withRTP, validation is failing with
    Unable to run or validate this rule (ConsolidateTestRTP) against the following location: Planning/servername/appname/dbname for user abcd.
    Detail:Exception occurred. Please check your log file for details.
    2009-04-09 12:35:58,803 WARN AWT-EventQueue-0 com.hyperion.hbr.core.MetadataManagerServerRemote - Cannot retrieve connected root element for Planning/servername/appname/dbname
    Restarted RMI,Planning,EAS services. Logged into planning first and then to EAS. Still getting error
    2009-04-09 12:41:39,881 FATAL AWT-EventQueue-0 com.hyperion.hbr.client.LauncherWindow - Exception:
    With thought of may be the syntax of RTPs in BR changed, deleted RTP in the code but still BR has RTP (I mean BR has RTP and is not used in calculations), still getting error during validation. I can create new BRs and associate to outline.
    Thanks

  • I have multiple computers set up on homesharing under one itunes account. Can I use face time on all of them or do i need seperate accounts for each?

    I have multiple computers set up on homesharing under one itunes account. Can I use face time on all of them or do i need seperate accounts for each?

    I'd suggest you consider a cloud-based file sharing service such as Dropbox for this sort of use. iOS is not designed to be synched with more than one iTunes library at a time, so attempting to use it as you describe, connecting a single iPad to multiple computers, almost certainly won't work as you intend and is likely to cause nothing but headaches.
    Regads.

  • I have an iPad, iPod touch, iPod classic. I tried using one account for allow only not being able to sign on with my touch. Do I  need separate accounts for each?

    Do I need separate accounts for each?
    I have an iPad, iPod classic and an iPod touch.
    Can not sign on to App Store with the touch.
    So how can I set up a new account?

    Error message -can not sign into App Store
    Same message for iTunes
    I use my iPad most of the time but would like to ad app to the touch

  • Need new spool for each Start_form

    Hi all
    I have a print program for RFFOAVIS. i have different orders spools shoould be like one spool for each order. but now i am seeing only one spool for all orders.
    First Open_form, then I am calling the same for each order using start_form and end_form. then i am calling Close_form.
    Though in Open_form i pass options tdnewid as 'x' or space i am seeing only one spool for all the orders.
    but i need New spool for each order.
    Regards
    helpful answeres are surely rewarded

    Hi Priyanka,
    you have to 'X' the field TDFINAL in OPTIONS too.
    Try this.
    Cheers,
    Joost

  • Select count(*) for each row of a table

    Hello All,
    Following query gives a statistics for each user (how many items he owns, home many tickets authored, how many objects he is subscribed to etc...)
    select auser.userid,
    (select count(*) from item where owner like '%' || auser.id || '%') ITEM_OWNER_CNT,
    (select count(*) from tkt where originator = auser.id) TKT_ORIGINATE_CNT,
    (select count(*) from tkt where assigned_to = auser.id) TKT_QA_CNT,
    (select count(*) from tkt where create_user = auser.id) TKT_AUTHOR_CNT,
    (select count(*) from subscriptions where subscriber_id = auser.id) SUBSCRIPTION_CNT
    from
    user auser
    I was not happy with the performance of this query, so I tried the same using group by. The performance was even worse.
    Is there any other option for me to try? Please advice.
    Thanks,
    Sathish

    Hi, Sathish,
    As SBH said, a lot depends on your data. Please post some sample data (CREATE TABLE adn INSERT statemetns) for all tables, and the results you want from that data. Describe and give examples of any relationships that are not one-to-one..
    You probably want to do joings, like SBH suggested, rather than scalar sub-queries.
    The connection between the auser and item tables
    (select count(*) from item where owner like '%' || auser.id || '%') ITEM_OWNER_CNT,is very suspicious. Perhaps the item table is poorly designed, and the query would be faster if that table were changed. Is changing the design of the item table an option?
    You should be able to get all the information from the tkt table in one pass. It looks like you need to unpivot the data, so instead of one row per ticket (with 3 different people connected to it), there are 3 rows per ticket, each with only 1 person referenced. This is not necessarily a bad table design. Unpivoting, even more than most other things, depends on your database version, so you'll have to tell what version of Oracle you're using.

  • Get the Count for each row

    I'm trying to get the count for each row to total count for each month
    Something like this
    Hardware     |      Jan
    Monitors       |       5
    Processors   |      137
    Printers        |      57
    etc........
    How can I write a query for this. I can get the Hardware column but don't know how to get the next column.

    If you can provide more data like sample input DML statements it would have been wonderful..
    Assuming is , you need a pivot. Here is an article on basic Pivot..
    http://sqlsaga.com/sql-server/how-to-use-pivot-to-transform-rows-into-columns-in-sql-server/
    something like this may be..
    DECLARE @Input TABLE
    Hardware VARCHAR(20),
    [Date] VARCHAR(20)
    INSERT INTO @Input VALUES('Monitor', '01/01/2014'), ('CPU', '01/01/2014'), ('Monitor', '01/03/2014')
    , ('ABC', '01/01/2014'),('Monitor', '02/01/2014')
    ;WITH CTE AS
    SELECT Hardware, LEFT(DATENAME(M, [Date]),3) AS [MonthName] FROM @Input
    SELECT *
    FROM
    SELECT Hardware, [MonthName], COUNT(Hardware) AS Count FROM CTE GROUP BY Hardware, [MonthName]) a
    PIVOT (MAX([Count]) FOR [MonthName] IN ([Jan], [Feb])) pvt
    Please mark as answer, if this has helped you solve the issue.
    Good Luck :) .. visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.

  • For each row of a table call a pl/sql function

    Hi,
    i have a search form in adf like this:
    parameter1:___
    parameter2:____
    buttonSearch
    Table with results
    field1 field2 field3
    row1 ------ --------- -------
    row2 ------ --------- -------
    row3 ------ --------- -------
    The user inputs the parameters 1 and 2 then press buttonSearch and the query execute and returns rows 1 to 3.
    What i need is for each row call pl/sql function and passed the parameter 1 and 2 and field 1 to 3 (plsql function recives 5 parameters (parameter1, parameter2, field1 , field2 and field3) )
    my buttonSearch call a java class that execute ExecuteWithParamters method.
    I create the call to my plsql function on Application module class and then export as a java interface.
    So i have the function to use in the viewcontroller layer, but i don't know where to use it, and how to pass the paramters: the parameter 1 and 2 that user inputs and the row fields.....
    any ideas....
    thanks!!

    Hi,
    for this you need to call the PLSQL function upon table rendering, which means that you need a field in the table referencing a managed bean. In the managed bean you can use #{row} and resolve it using a ValueExpression. #{row} gives you access to the current rendered row (this is why you need to do it when the table renders) and thus allows you to call getAttribute(name) to get the values of field 1 - 3. The search field value you should get through the bindings reference (assuming the search form uses ADF). Then you create an operation binding for the executeWithParameters and call operationBindingName.getParamsMap().put(argname, argvalue); on it.
    Frank
    Ps.: I am concerned about the performance you get and wonder if it isn't possible to create a transient attribute that executes the function and displays the results. As I understand, the search parameters are only to filter the result set, which you still can do

  • Check Box for each row in report -- all rows deleting when pressing DELETE

    Hello experts! I have set up a report with a check box for each row. When I click the DELETE button to delete the selected rows, every single one of the rows get deleted...even the ones that are not selected. I have my process point set to "On Submit - After computations and validations".
    This is my delete process (SHG is the table and SHG_ID is the primary key):
    FOR i in 1..HTMLDB_APPLICATION.G_F01.count
    LOOP
    DELETE FROM SHG
    WHERE SHG_ID = HTMLDB_APPLICATION.G_F01(i);
    END LOOP;
    Also, I've added to query in the region source this line:
    htmldb_item.checkbox(1, SHG_ID) del,
    Where does the "1" come into this? Not sure what I am doing wrong!
    Message was edited by:
    user477193
    Message was edited by:
    user477193

    The 1 (first argument to all the htmldb_item.* APIs) corresponds to the array number in htmldb_application.g_fNN. So 1 will populate array g_f01, 2 will populate g_f02 and so on.
    Your code seems fine, it should delete only the checked rows. Are you sure there is no other process on the page that might be deleting the rows? See if you can put up an example on htmldb.oracle.com

  • Prompting user for input for each row in the report

    Is it possible to have a user entered field for each row in a report ?
    ie. have a report with empolyee number, employee name, title, salary, and comment. Select of of these columns from the SQL query except for the comment, and then when the report is generated have the user be prompted for what the want the comment to be for every employee in the report?

    That is, have the user generating the report enter in a different comment for EACH employee. (ie Joe Smith's comment is "good worker and John Smith's comment is "come to work late on Tuesdays and Thursdays", Sally Jones' comment is "Expert in C++" etc.) as the reports is being built for each row returned from the query.

  • SQL merge and after insert or update on ... for each row fires too often?

    Hello,
    there is a base table, which has a companion history table
    - lets say USER_DATA & USER_DATA_HIST.
    For each update on USER_DATA there has to be recorded the old condition of the USER_DATA record into the USER_DATA_HIST (insert new record)
    - to have the history of changes to USER_DATA.
    The first approach was to do the insert for the row trigger:
    trigger user_data_tr_aiu after insert or update on user_data for each rowBut the performance was bad, because for a bulk update to USER_DATA, there have been individual inserts per records.
    So i tried a trick:
    Instead of doing the real insert into USER_DATA_HIST, i collect the USER_DATA_HIST data into a pl/sql collection first.
    And later i do a bulk insert for the collection in the USER_DATA_HIST table with stmt trigger:
    trigger user_data_tr_ra after insert or update on user_dataBut sometimes i recognize, that the list of entries saved in the pl/sql collection are more than my USER_DATA records being updated.
    (BTW, for the update i use SQL merge, because it's driven by another table.)
    As there is a uniq tracking_id in USER_DATA record, i could identify, that there are duplicates.
    If i sort for the tracking_id and remove duplicate i get exactly the #no of records updated by the SQL merge.
    So how comes, that there are duplicates?
    I can try to make a sample 'sqlplus' program, but it will take some time.
    But maybe somebody knows already about some issues here(?!)
    - many thanks!
    best regards,
    Frank

    Hello
    Not sure really. Although it shouldn't take long to do a test case - it only took me 10 mins....
    SQL>
    SQL> create table USER_DATA
      2  (   id      number,
      3      col1    varchar2(100)
      4  )
      5  /
    Table created.
    SQL>
    SQL> CREATE TABLE USER_DATA_HIST
      2  (   id      number,
      3      col1    varchar2(100),
      4      tmsp    timestamp
      5  )
      6  /
    Table created.
    SQL>
    SQL> CREATE OR REPLACE PACKAGE pkg_audit_user_data
      2  IS
      3
      4      PROCEDURE p_Init;
      5
      6      PROCEDURE p_Log
      7      (   air_UserData        IN user_data%ROWTYPE
      8      );
      9
    10      PROCEDURE p_Write;
    11  END;
    12  /
    Package created.
    SQL> CREATE OR REPLACE PACKAGE BODY pkg_audit_user_data
      2  IS
      3
      4      TYPE tt_UserData        IS TABLE OF user_data_hist%ROWTYPE INDEX BY BINARY_INTEGER;
      5
      6      pt_UserData             tt_UserData;
      7
      8      PROCEDURE p_Init
      9      IS
    10
    11      BEGIN
    12
    13
    14          IF pt_UserData.COUNT > 0 THEN
    15
    16              pt_UserData.DELETE;
    17
    18          END IF;
    19
    20      END;
    21
    22      PROCEDURE p_Log
    23      (   air_UserData        IN user_data%ROWTYPE
    24      )
    25      IS
    26          ln_Idx              BINARY_INTEGER;
    27
    28      BEGIN
    29
    30          ln_Idx := pt_UserData.COUNT + 1;
    31
    32          pt_UserData(ln_Idx).id     := air_UserData.id;
    33          pt_UserData(ln_Idx).col1   := air_UserData.col1;
    34          pt_UserData(ln_Idx).tmsp   := SYSTIMESTAMP;
    35
    36      END;
    37
    38      PROCEDURE p_Write
    39      IS
    40
    41      BEGIN
    42
    43          FORALL li_Idx IN INDICES OF pt_UserData
    44              INSERT
    45              INTO
    46                  user_data_hist
    47              VALUES
    48                  pt_UserData(li_Idx);
    49
    50      END;
    51  END;
    52  /
    Package body created.
    SQL>
    SQL> CREATE OR REPLACE TRIGGER preu_s_user_data BEFORE UPDATE ON user_data
      2  DECLARE
      3
      4  BEGIN
      5
      6      pkg_audit_user_data.p_Init;
      7
      8  END;
      9  /
    Trigger created.
    SQL> CREATE OR REPLACE TRIGGER preu_r_user_data BEFORE UPDATE ON user_data
      2  FOR EACH ROW
      3  DECLARE
      4
      5      lc_Row      user_data%ROWTYPE;
      6
      7  BEGIN
      8
      9      lc_Row.id   := :NEW.id;
    10      lc_Row.col1 := :NEW.col1;
    11
    12      pkg_audit_user_data.p_Log
    13      (   lc_Row
    14      );
    15
    16  END;
    17  /
    Trigger created.
    SQL> CREATE OR REPLACE TRIGGER postu_s_user_data AFTER UPDATE ON user_data
      2  DECLARE
      3
      4  BEGIN
      5
      6      pkg_audit_user_data.p_Write;
      7
      8  END;
      9  /
    Trigger created.
    SQL>
    SQL>
    SQL> insert
      2  into
      3      user_data
      4  select
      5      rownum,
      6      dbms_random.string('u',20)
      7  from
      8      dual
      9  connect by
    10      level <=10
    11  /
    10 rows created.
    SQL> select * from user_data
      2  /
            ID COL1
             1 GVZHKXSSJZHUSLLIDQTO
             2 QVNXLTGJXFUDUHGYKANI
             3 GTVHDCJAXLJFVTFSPFQI
             4 CNVEGOTDLZQJJPVUXWYJ
             5 FPOTZAWKMWHNOJMMIOKP
             6 BZKHAFATQDBUVFBCOSPT
             7 LAQAIDVREFJZWIQFUPMP
             8 DXFICIPCBCFTPAPKDGZF
             9 KKSMMRAQUORRPUBNJFCK
            10 GBLTFZJAOPKFZFCQPGYW
    10 rows selected.
    SQL> select * from user_data_hist
      2  /
    no rows selected
    SQL>
    SQL> MERGE
      2  INTO
      3      user_data a
      4  USING
      5  (   SELECT
      6          rownum + 8 id,
      7          dbms_random.string('u',20) col1
      8      FROM
      9          dual
    10      CONNECT BY
    11          level <= 10
    12  ) b
    13  ON (a.id = b.id)
    14  WHEN MATCHED THEN
    15      UPDATE SET a.col1 = b.col1
    16  WHEN NOT MATCHED THEN
    17      INSERT(a.id,a.col1)
    18      VALUES (b.id,b.col1)
    19  /
    10 rows merged.
    SQL> select * from user_data_hist
      2  /
            ID COL1                 TMSP
             9 XGURXHHZGSUKILYQKBNB 05-AUG-11 10.04.15.577989
            10 HLVUTUIFBAKGMXBDJTSL 05-AUG-11 10.04.15.578090
    SQL> select * from v$version
      2  /
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Linux: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - ProductionHTH
    David

  • Calling a stored procedure for each row returned

    I need to call a stored procedure for each row returned by a repeating frame. I called the stored procedure from the repeating frame format trigger, but that did not work( it did no populated the tables populated by the stored procedure)
    How can I call a stored procedure for each row returned by a repeating frame.
    Thank you

    Include it as a formula column in your data model.

  • Adding a check box control for each row of data in a DataModel

    Hi all,
    I need to add a checkbox control for each row of data on a DataModel object.
    I have a "commandButton" at the bottom of DataModel, and whenever someone checks some of the rows on that list of rows,
    I need to get the selected dataModel(fragment of the list) in my backing bean.
    How do I achieve this functionality in JSF?
    Thanks,
    Meghasyam.

    Hi all,
    I need to add a checkbox control for each row of data
    on a DataModel object.
    I have a "commandButton" at the bottom of DataModel,
    and whenever someone checks some of the rows on that
    list of rows,
    I need to get the selected dataModel(fragment of the
    list) in my backing bean.
    How do I achieve this functionality in JSF?
    Thanks,
    Meghasyam.You'll want to have a wrapper class as suggested above, which has a "selected" boolean in it. Then use the "binding" attribute of the h:selectBooleanCheckbox component to bind the checkbox to that property... Make the property public and specify the properties exact name in the binding... bindings do not append "get" to the EL.
    Here is an example of what your table might look like... This code would display the list of names with a checkbox to the left of each name... When the check box is selected, the "selected" property of that wrapper class is set to true or false as needed. Then when the form is submitted, and you are inside your actionListener or action method call, you can look through your collection of wrapper classes asking each one if it was selected or not... Then do whatever you want with them... In this example, replace "myBackingBean" with the name of your backing bean, and "names" with the name of the method in your backing bean which returns the collection of wrapper classes... create a flag "public boolean selected" or similar in your wrapper class..
    <h:dataTable id="namestable"
    value="#{myBackingBean.names}"
    var="aName">
    <h:column>
    <h:selectBooleanCheckbox binding="#{aName.selected}"/>
    <h:outputText value="#{aName.nameText}"/>
    </h:column>
    </h:dataTable>
    Let me know if that isn't clear enough and I'll see if I can find a better way to explain it...
    -Garrett

  • In a hierarchical query, is it possible for a row to have more than one immediate ancestor?

    Hi
    Question:
    In a hierarchical query, is it possible for a row to have more than one immediate ancestor?
    Answer:
    No
    No?  Surely, it's yes?
    Thanks,
    Jason

    As Frank pointed out already hierarhical most often means a tree (data structure) to deal with.
    There must usually be just one boss (the root) in which case the answer is no.
    Something to read: http://en.wikipedia.org/wiki/Tree_(data_structure)
    You can find out Solomon spoke about a generalization therein.
    Related to forum troubles:
    If I login first thing after reaching forum, the behaviour is rather consistent - I'm allowed to post answers, otherwise ...
    Regards
    Etbin

  • Formula Variable for Each Row

    Is there any way to make the formula variable take different values for different rows in a given report?  Our requirement is that each sales division in US will be approved of a certain percentage of rebate that it can use.  In the report one should see the net sales value after considering the % rebate.  For e.g., US has East, West, South, Central and Midwest Sales Divisions.  There are 5 product lines, prod1 to prod5.  The client will maintain a Z table for all the combinations of Sales division and Product line (i.e., East & prod1 - 10%, East & prod2 - 3%...  Midwest & prod5 - 5%).  Actual sales done in each sales division will not have these rebates considered.  So at the corporate level when the report is executed, the query should be able to apply the corresponding values to the actual sales.  Is it possible with BEx?  As far as I know, the user exit is not executed for each row.  Hope I was lucid enough to explain the problem at hand and if anyone has any effective ideas on this issue, please post your thoughts here.  Thanks a ton in advance.
    IS

    Hi,
    I doubt that you'll be able to do it in BEx.
    The easiest way is to include a rebate into your infoprovider.
    Best regards,
    Eugene

Maybe you are looking for