Flatten a hierarchy
I'm trying to flatten a hierarchy. I have a table with a parent/child relationship in the following order
Parent_ID Child_ID
1234 1234
1234 1235
1235 1236....etc
I would like to transform the hierarchy to
1234 , 1235, 1236
If I use a sys_connect_by I get
1234;
1234;1235
1235;1235;1236
Thanks
Khetani
Starting from Oracle 10 you can use CONNECT_BY_ISLEAF operator to filter out unwanted rows:
WITH HIERARCHY AS (
SELECT LEVEL + 1233 PARENT_ID, LEVEL + 1233 + 1 CHILD_ID
FROM DUAL CONNECT BY LEVEL < 6
SELECT SYS_CONNECT_BY_PATH( PARENT_ID, ';') PARENT_ID
FROM HIERARCHY
WHERE CONNECT_BY_ISLEAF = 1
START WITH PARENT_ID = 1234
CONNECT BY PRIOR CHILD_ID = PARENT_ID;
PARENT_ID
;1234;1235;1236;1237;1238
Similar Messages
-
How to write SQL query to flatten the hierarchy
Hi,
I have person table which has recursive hierarchy and I wish to flatten it upto 5 levels.
I am using Oracle10g and I have written following query to flatten the hierarchy
SELECT
ID,
level lvl,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 1) AS level_1,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 2) AS level_2,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 3) AS level_3,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 4) AS level_4,
REGEXP_SUBSTR (SYS_CONNECT_BY_PATH (fname||' '||lname, '/'), '[^/]+', 1, 5) AS level_5
FROM cmt_person
CONNECT BY manager_id = PRIOR id
and level<=5
The person table have more than a million records.
Here I am getting the correct output but this query is taking a lot of time to run.
I am looking at a SQL query without use of connect by to get the required output.
To recreate the issue, you can use this query in HR schema of Oracle and use Employees table.
Any help would be greatly appreciated.
Thanks,
RaghvendraI tried to rewrite the query without using regular expression and connect by. Here is the code:
SELECT
cmt_person.id ,
cmt_person.fname as mgr1,
case when cmt_person2.manager_id=cmt_person.id then (cmt_person2.fname ) end as mgr2,
case when cmt_person3.manager_id=cmt_person2.id then (cmt_person3.fname ) end as mgr3,
case when cmt_person4.manager_id=cmt_person3.id then cmt_person4.fname end as mgr4,
case when cmt_person5.manager_id=cmt_person4.id then cmt_person5.fname end as mgr5
FROM
cmt_person,
cmt_person cmt_person2,
cmt_person cmt_person3,
cmt_person cmt_person4,
cmt_person cmt_person5
WHERE
cmt_person2.manager_id(+)=cmt_person.id and
cmt_person3.manager_id(+)=cmt_person2.id and
cmt_person4.manager_id(+)=cmt_person3.id and
cmt_person5.manager_id(+)=cmt_person4.id
order by 2,3,4
I got following output:
emplo000000000200100 Bobby Khasha Rahul Rajesh
emplo000000000200099 Bobby Khasha Rahul Ajay
emplo000000000200101 Bobby Khasha Rahul Swati
emplo000000000200320 Bobby Khasha Rahul Jinesh
emplo000000000201231 Bobby Khasha Test123
emplo000000000201230 Bobby Khasha User1_Domain
emplo000000000201227 Bobby Khasha User1_World
emplo000000000200104 Bobby Khasha Yitzik Natalia
emplo000000000200103 Bobby Khasha Yitzik Andrew
total 9 rows
But this is partially correct output. I want output like this:
emplo000000000200097 Bobby
emplo000000000200087 Bobby Khasha
emplo000000000200102 Bobby Khasha Yitzik
emplo000000000200103 Bobby Khasha Yitzik Andrew
emplo000000000200104 Bobby Khasha Yitzik Natalia
emplo000000000201227 Bobby Khasha User1_World
emplo000000000201231 Bobby Khasha Test123
emplo000000000201230 Bobby Khasha User1_Domain
emplo000000000200098 Bobby Khasha Rahul
emplo000000000200099 Bobby Khasha Rahul Ajay
emplo000000000200100 Bobby Khasha Rahul Rajesh
emplo000000000200320 Bobby Khasha Rahul Jinesh
emplo000000000200101 Bobby Khasha Rahul Swati
total 13 rows
Do you know what I should do to get this output.
Thanks,
Raghvendra -
Flatten family hierarchy to level 2, level 3, level 4 across
For example....
Family Hierarchy
Grandma 1
Mom 1
daughter 1
Grand daughter 1
Great Grand daughter 1
How do you flatten this one Field to say
Grandma Mom Daughter
Grandma 1 Mom1 Daughter 1 ect.... across the top
Do you create a selection in the row pane or a formula for the same field several times? Thanks!!!!Hello Experts,
I am also facing the same issue.I want to get a flat file output from BW system which
includes the hierarchy fields from 2 different hierarchies as columns of flat file with some other fields.
Is there any way to do this ?
Regards,
Vikas Arya -
How to flatten keyword hierarchy?
For about a year I had all my keywords organized into a nice hierarchy, but after moving projects between libraries and computers my keywords are in chaos because keyword hierarchy information is not included in exported projects. I have keywords repeated with one nested in a folder and its copy sitting at the top level (with photos assigned to both).
After seeing what a mess moving projects causes, I'd like to eliminate the hierarchies and just keep a flat list. Does anyone know of a way to flatten the structure without having to drag each keyword out of its folder? I tried deleting the Keywords.plist file, but it gets rebuilt with the hierarchy intact. Any help is appreciated!One thing that might be a factor is the way Aperture keeps keywords active even if you move or delete them in the keywords list.
In order to have control over your keyword list, you need to delete existing keywords from your images. As Steve Weller points out in his articles, Aperture keeps keyword information in several places, not just the keyword list, and you need to deal with all of the places in order to control the hierarchy of the keyword list. For example, if you have set up custom keyword buttons, they store the keyword in the hierarchy of the time the button was created, so each time you use the button, you are making Aperture "remember" the hierarchy, even if you have later moved the keyword.
But your issue is that you have many images using keywords. When a keyword is used in an image, it is stored with the image; Aperture remembers the hierarchy of the keyword. This means that if you later move that keyword in the keyword list, Aperture considers the moved keyword to be separate and independent of the keyword previously used with an image. You can see this in the smart album display of keywords, which is based on actually used keywords. If you are having this problem, then keywords will appear more than once in the smart album keyword list. If you want to keep the same spelling of a keyword that has been moved in the keyword list, the only solution I know of is, frankly, a bit tedious:
1. use the smart album to select all images using the unwanted version of the keyword
2. then assign an "intermediate" (not identically spelled) keyword to those images,
3. then delete the unwanted version of the keyword from the images (use the keyword field in the control bar; option-tilde or click on the field, then type the name of the unwanted keyword, then shift-return to delete the keyword)
4. then assign the new version of the keyword to the images
5. then delete the intermediate keyword
Good luck! -
Flattening the hierarchial data
Hi,
I have the data in this format
ID NAME ParentID
CV1 CV1NAME CV
CVX1 CVX1NAME CV1
CVXX1 CVXX1NAME CVX1
CV2 CV2NAME CV
CVX2 CVX2NAME CV2
CVXX2 CVXX2NAME CVX2
How can i flatten this data into this format
CVID CVNAME CVXID CVXNAME CVXXID CVXXNAME
cv1 cv1name cvx1 cvx1name cvxx1 cvxx1name
cv2 cv2name cvx2 cvx2name cvxx2 cvxx2nameDECLARE @table TABLE (ID varchar(10), NAME varchar(20), ParentID varchar(10))
INSERT INTO @table (ID, NAME, ParentID) VALUES
('CV1', 'CV1NAME', 'CV'),
('CVX1', 'CVX1NAME', 'CV1'),
('CVXX1', 'CVXX1NAME', 'CVX1'),
('CV2', 'CV2NAME', 'CV'),
('CVX2', 'CVX2NAME', 'CV2'),
('CVXX2', 'CVXX2NAME', 'CVX2')
SELECT
t0.ID AS CVID,
t0.NAME AS CVNAME,
t1.ID AS CVXID,
t1.NAME AS CVXNAME,
t2.ID AS CVXXID,
t2.NAME AS CVXXNAME
FROM
SELECT ID, NAME, RIGHT(ID, 1) AS Num
FROM @table
WHERE ID LIKE 'CV[1-9]'
) AS t0
FULL JOIN
SELECT ID, NAME, RIGHT(ID, 1) AS Num
FROM @table
WHERE ID LIKE 'CVX[1-9]'
) AS t1 ON t1.Num = t0.Num
FULL JOIN
SELECT ID, NAME, RIGHT(ID, 1) AS Num
FROM @table
WHERE ID LIKE 'CVXX[1-9]'
) AS t2 ON t2.Num = t0.Num
A Fan of SSIS, SSRS and SSAS -
Hi
I have a parent child ragged hisrarchy. I want to build level wise dimension to be able to report using crystal .
The PC table I have is like
Child Parent
A11 A1
A12 A1
A111 A11
A112 A11
A113 A11
A121 A12
A122 A12
A1121 A112
A1221 A122
A1222 A122
I want to solve this as a level based hierarchy using universe like:
LEV1 LEV2 LEV3 LEV4
A1 A12 A122 A1222
A1 A12 A122 A1221
A1 A11 A112 A1121
A1 A12 A122
A1 A12 A121
A1 A11 A113
A1 A11 A112
A1 A11 A111
A1 A12
A1 A11
Can somebody please put some light on this how can I achieve this?
Thanks
AtulHi Atul,
You can use a Derived Table that flatten the hierarchy.
Here is a sample based on the example you provided, you just have to replace "child", "parent" and "myTable" strings by the corrseponding column and table names:
SELECT DISTINCT
Z.child,
A.parent_1,
A.child_1,
A.parent_2,
A.child_2,
A.parent_3,
A.child_3,
Z.parent AS parent_4,
Z.child AS child_4
FROM myTable Z,
SELECT DISTINCT
A.parent_1,
A.child_1,
A.parent_2,
A.child_2,
Z.parent AS parent_3,
Z.child AS child_3
FROM myTable Z,
SELECT DISTINCT
A.parent_1,
A.child_1,
Z.parent AS parent_2,
Z.child AS child_2
FROM myTable Z,
SELECT DISTINCT
parent AS parent_1,
child AS child_1
FROM myTable
WHERE parent = 0 OR parent IS NULL
) A
WHERE A.child_1 = Z.parent
) A
WHERE A.child_2 = Z.parent
) A
WHERE A.child_3 = Z.parent
UNION
SELECT DISTINCT
A.child_3 AS child,
A.parent_1,
A.child_1,
A.parent_2,
A.child_2,
A.parent_3,
A.child_3,
NULL AS parent_4,
NULL AS child_4
FROM myTable Z,
SELECT DISTINCT
A.parent_1,
A.child_1,
A.parent_2,
A.child_2,
Z.parent AS parent_3,
Z.child AS child_3
FROM myTable Z,
SELECT DISTINCT
A.parent_1,
A.child_1,
Z.parent AS parent_2,
Z.child AS child_2
FROM myTable Z,
SELECT DISTINCT
parent AS parent_1,
child AS child_1
FROM myTable
WHERE parent = 0 OR parent IS NULL
) A
WHERE A.child_1 = Z.parent
) A
WHERE A.child_2 = Z.parent
) A
WHERE A.child_3 NOT IN (SELECT parent FROM myTable)
UNION
SELECT DISTINCT
A.child_2 AS child,
A.parent_1,
A.child_1,
A.parent_2,
A.child_2,
NULL AS parent_3,
NULL AS child_3,
NULL AS parent_4,
NULL AS child_4
FROM myTable Z,
SELECT DISTINCT
A.parent_1,
A.child_1,
Z.parent AS parent_2,
Z.child AS child_2
FROM myTable Z,
SELECT DISTINCT
parent AS parent_1,
child AS child_1
FROM myTable
WHERE parent = 0 OR parent IS NULL
) A
WHERE A.child_1 = Z.parent
) A
WHERE A.child_2 NOT IN (SELECT parent FROM myTable)
UNION
SELECT DISTINCT
A.child_1 AS child,
A.parent_1,
A.child_1,
NULL AS parent_2,
NULL AS child_2,
NULL AS parent_3,
NULL AS child_3,
NULL AS parent_4,
NULL AS child_4
FROM myTable Z,
SELECT DISTINCT
parent AS parent_1,
child AS child_1
FROM myTable
WHERE parent = 0 OR parent IS NULL
) A
WHERE A.child_1 NOT IN (SELECT parent FROM myTable)
Regards,
Didier -
BW upgrade EHP1, data uploads should stop?
Dear experts,
we have plan for system upgrade. current system is BW 7.0 sp 17, Now planned for EHP1 and SP9.
I know the there some post upgrade activities, which include consistancy check for objects (infoobject, transfer rules, cubes,dso, etc...)
Could some one pls confirm, do we need to stop the data uploads/stop process chains during system upgrade?
Thanks in advance!
Best Regards,
MannuHi Ingo,
RSRT was giving proper results. We have now implemented few SAP notes and the issues got resolved.
The following are the notes:
1499233 - MDX:bXML flattening, unbalanced hierarchy, empty columns
1485648 - MDX: bXML flattening and hierarchies and displaced columns
1446245 - MDX: Error when RSR_MDX_BXML_GET_GZIP_DATA is called
1441767 - MDX: No data for bXML if only ALL member is requested
1438091 - MDX: basXML: Object MEASURE 0 not found
1435844 - MDX:Wrong no. decimal places for basXML flattening interface
1432162 - MDX: Flattening problems when using hierarchies
1420169 - MDX: bXML flattening: Subsequent note
1411491 - MDX: bXML flattening in transac. MDXTEST: Selecting packages
1404328 - MDX: bXML flattening and PROPERTIES: Columns overwritten
Thanks for your inputs.
Regards,
shesha. -
Changing into parent-child relation
Hi,
I have a table which has the following fields and the table gives the identifier for parent-child relation:
ID
PARENT ID
structure
parentkey_field
foreignkey_field
Fields[]
data[]
1.ID is the numbering like 1,2,3...
2.PARENT ID will give the relation like which structure is the child for which structure.
Example: I have two structures HEADER and ITEM.Then the IDs will be 1 and 2.The parent id for ITEM will be 1(ID OF HEADER).
3.Structure is the header/item structure names.
4.parentkey_field and foreignkey_field are defining the header-child relation identifier fields.
5.fields[] is the table with all the fields of header and item.
6.DATA[] is the table having the data of header/item fields.
Now my requirement is to flatten this table into one more table(with field structure_name,fieldname,value) with parent-child relation.
Can anyone suggest something.It would be better if you posted the code you tried first and then having others help you with what's wrong. Flattening a hierarchy is not that difficult; why don't you give it a shot first?
-
Value based hierarchies, Skip Level Hierarchies
Hello,
I need to create hierarchy based on column value , Please give me some details on implementing value based hierarchies and skip level hierarchies in obiee 10.1.3.
Thank youWith 10.3 i do not think we have a inbuilt solution to build value based hierarchies, you have to flatten the hierarchy and build a level based hierarchy.
OBIEE 11g has value based hierarchies feature.
refer this one for 11g.
http://sandybi.wordpress.com/2010/08/17/hierarchies-in-obiee-11g-part-1/
- Madan
Edited by: Madan Thota on Sep 14, 2010 11:15 AM -
Sort by Name in style palettes (INDD CS3)
Every time I add new styles to the Paragraph, Character, or Object styles palettes, I have to manually re-set the display to 'Sort by Name'. This was never necessary in CS2. Is there any way to set this as a default in CS3?
It would be fine to have them be unsorted by default as long as there was a way to sticky toggle sorting on for those that prefer to work the old way. IMHO, the new folder system is something of an accident waiting to happen too if you don't set up for it from the very beginning.
- Each folder is independent of every other folder or the top level, meaning you can have an "H1" head style in each folder and all of them be defined differently.
- If you started with a flat hierarchy from CS2 imported into CS3, you can rapidly get your CS3 working documents into a hozed state if you aren't aware of the first point. If you organize your styles into folders, but then open an old CS2 document again and copy stuff across from it, CS3 will see that the old style(s) associated with whatever you copy don't exist at the top level (eg you copied them all into the Chapter Opener sub-folder) and it will then re-import the styles back into the top level again, which means that you now have your original CS3 stuff linked to a style in a subfolder and the stuff you just copied in linked to a similar style at the top level.
Until you do some clean-up and figure out which style is supposed to replace over which, you can no longer rely on all your styled text being the same or on all your styled text changing when you change a style.
- IIRC, there is no easy way to flatten the hierarchy, such that you can tell if you have duplicate names in various subfolders, which means that it's fairly incompatible with export to INX back to CS2. (I have a vague recollection of doing this experiment, but I could be wrong on this point.) -
I have written the following SQL to get all the child names:
WITH xml_table AS
(SELECT xmltype('
<root>
<child name="name1">
<grandchild name="name11">
<greatgrandchild name="name111"/>
<greatgrandchild name="name112"/>
</grandchild>
<grandchild name="name12"/>
</child>
<child name="name2">
<grandchild name="name21"/>
<grandchild name="name22"/>
</child>
</root>') object_value FROM dual)
select po.*
from xml_table p,
xmltable('/root' passing p.object_value columns child varchar2(100) path 'child/@name') po;
I understand there are multiple "child" nodes under "root". So how to list them?
I want to store all the "child" names in one table, "child" nodes and their "grandchild" in another table, "grandchild" and "greatgrandchild" in another table. What should be SQLs?Here's how you can "flatten" the hierarchy, it should give you some pointers to achieve your requirement :
WITH xml_table AS (
SELECT xmltype(
'<root>
<child name="name1">
<grandchild name="name11">
<greatgrandchild name="name111"/>
<greatgrandchild name="name112"/>
</grandchild>
<grandchild name="name12"/>
</child>
<child name="name2">
<grandchild name="name21"/>
<grandchild name="name22"/>
</child>
</root>'
) object_value
FROM dual
SELECT po1.child,
po2.grandchild,
po3.greatgrandchild
FROM xml_table p,
XMLTable(
'/root/child'
passing p.object_value
columns
child varchar2(100) path '@name',
childs xmltype path '.'
) po1,
XMLTable(
'/child/grandchild'
passing po1.childs
columns
grandchild varchar2(100) path '@name',
grandchilds xmltype path '.'
) (+) po2,
XMLTable(
'/grandchild/greatgrandchild'
passing po2.grandchilds
columns
greatgrandchild varchar2(100) path '@name'
) (+) po3
;To deal with missing descendants, note the use of the outer join with the last two XMLTABLEs. -
Hi, I defined dimensional hierarchy of 6 levels in Repository from a table.
I have a flat table in database which represents the hierarchy and is as below:
some levels have no sublevels and some have. the max levels is five, lets say.
When a level has no sub level, there is null value in the field.
level 1 is top most and level 5 being the lowest level. as below:
http://img828.imageshack.us/img828/2531/29116555.jpg
I have one more table, where I have employee name, employee number. I joined these two tables and
I made a dimensional hierarchy combining these two tables.
In the report, I must see, employee details as child to the last existing level.
So I included employee name as detail for that hierarchy.
Now my problem is,
Ifthere is no sub level for particular level, it is noted as null in the table, so I should see employee name after that level.
That is, when a level is ended, and has no more further levels, it should display employee name as its child level and followed by measure value for that.
For example, in the report, I have to see as:
http://img101.imageshack.us/img101/5760/43637216.jpg
When I drill A, it should show me levels, a,b,c,d,empname
when I again click c, since c has no sub levels, it should retrieve employee name as drilldown.
Hope I am clear. I am not able to implement this,
Please help me.Your describing a ragged hiearchy that is not supported out of the box in OBIEE 10G
Your options :
1) Flatten the hierarchy as part of your ETL process, your level 2 'c' would be populated all the way down the hierarchy, no big deal really, just more columns in the report until you get to Employee Name + Number or whatever you have on your detail / leaf level.
2) use an nvl() function of whatever your source db supports and return a 'no child' or similar value literal string, at least the users will know they hit the bottom of the real data (level 2 in this case) and are now drilling through to the detail level. You could get clever and use a case statement on the value and offer some form of GOURL to another report to stop this drilling (but it wont give you drill in place on the dashboard, it will be a new window)
3) 11G is out as of yesterday, I think this supports ragged hierarchies now does it not ? I've not had a proper play yet, my colleague has been beta testing but some of us have to bill proper days ;-) -
Calculating DIrect and indirect reports for managers
Hi,
I have a flattened manager hierarchy. I want to calculate the direct and indirect reports for every manager. how can i do this.
Thanks
Edited by: user599926 on Apr 18, 2010 4:01 PMI've never worked with Access as a database, before. But, if this were SQL (and it might work), then, yes, a LEFT OUTER JOIN would do the trick.
SELECT ta.Lastname, ta.Firstname, ta.EmpID, ta.MgrEmpID, ta.Email, ta.Location
FROM TableA ta LEFT OUTER JOIN TableA tb ON tb.MgrEmpID = ta.EmpID
ORDER BY ta.Lastname, ta.Firstname, tb.Lastname, tb.FIrstname
Or something like that.
^_^ -
Flattened to Parent Child Hierarchy
Hi,
My client has a little different requirement from normal.
We want to convert the flattened version of hierarchy to parent child format. Does anyone have any sql script or idea how we can achieve this quickly?
Thanks in advance.http://www.rittmanmead.com/2007/06/21/obiee-data-modeling-tips-3-ragged-hierarchies/
might be helpful. -
Flatten Hierarchy By Filtering Out Unnecessary Limbs
We have numerous hierarchies in our organization that faciliate various reporting views. Some of those views are slight variations of one another. One common theme that we have encountered is the need for the same basic view, but with less granularity in terms of rollups.
Example (alphas are limbs, numerics are leafs):
Master Hierarchy
A
B
C
1
2
D
3
4
E
5
6
We need to derive a custom export that eliminates all the in-between nodes and just rolls all the leafs up to Node A.
A
1
2
3
4
5
6
This type of flattening may happen at any level within the hierarchy (not necessary all rolling up to the top level). So we need a somewhat dynamic way to achieve this. I am thinking that a property could be built to flag certain key levels that are required for the custom export, but still not sure how to get those key levels flagged as parents for the leafs, regardless of which level the leaf is at in the Master hierarchy.
Any thoughts?Hi Denzz,
What kind of OR condition could be written that would not only filter certain limb nodes out, but also realign the parent/child relationships accordingly. For example:
Master Hierarchy
A
B
C
1
2
D
3
4
E
5
6
We need to derive a custom export that eliminates rollups C & D.
A
B
1
2
3
4
E
5
6
I can understand creating a formula/property to filter in or out certain rollups. But we would also need to effectively change the parent value of 1, 2, 3, 4 to B (instead of C or D). Are you suggesting a formula that checks the immediate parent for the filter property, and if not found, then go to grandparent, then next grandparent and so on until found?
Maybe you are looking for
-
HT4918 what happen with the documents in Idisk, how can i see them in iCloud?
what happen with the documents that i have in my idisk?? how can i move them into icloud??
-
What is the first step of an abaper after getting the FDS
what is the first step of an abaper after getting the Functional Document ?what we have to do? how to analyze it , and how to face such interview questions on FDS TDS . any resource available.
-
Hi, We created return order with ref. to previous invoice and we changed excise duties. with ref. to that we done retun delivery, at the time of creating invoice system is not picking excise duties as per my return order it is picking that duties fro
-
Easiest way to see the source of photos ?
Still having trouble ! If on a webpage something is missing hows the easiest way to see the file that should be in the empty place ? Everything works fine on dreamweaver but when i uploaded it all went to pot ! The htm is there but no photos or inter
-
Hi all, I am totaly new with Flex, and need your help. I have a ArrayCollection getMD that saved the field passed from coldfusion cfc ++++++++++++++++++++++++++++++ <cfcomponent output="false"> <cffunction name="getMD" access="remote" returntype="que