Complex Query -Parent/Child
Dear All,
I have a strange requirement of generating report from a table, which has 2 columns trace element and trace reference where the relationship between trace reference and trace element is 1-Many. i.e each trace reference may have may childs and Each child may inturn may act as parent and it can have child records
WITH t AS
SELECT 'BSC/SR/0001/0002' AS trace_reference,'BSC/DS/0007/0013' AS trace_element from dual
UNION ALL
SELECT 'BSC/SR/0001/0002','BSC/DS/0004/0001' FROM dual
UNION ALL
SELECT 'BSC/SR/0001/0002','BSC/DS/0007/0033' FROM dual
select * from tOutput
trace Reference trace_element
BSC/SR/0001/0002 BSC/DS/0007/0013
BSC/SR/0001/0002 BSC/DS/0004/0001
BSC/SR/0001/0002 BSC/DS/0007/0033
Each of the trace element will inturn have some child records like below
WITH t1 AS
SELECT 'BSC/DS/0007/0013' AS trace_reference,'BSC/US/0003/0008' AS trace_element FROM dual
UNION ALL
SELECT 'BSC/DS/0007/0013','BSC/STC/0008/0002' FROM dual
SELECT * FROM t1Ouput
trace Reference trace_element
BSC/DS/0007/0013 BSC/US/0003/0008
BSC/DS/0007/0013 BSC/STC/0008/0002And the child record "BSC/US/0003/0008" May also have some child records.
WITH t3 AS
SELECT 'BSC/US/0003/0008' AS trace_reference,'BSC/UTC/0015/0003' AS trace_element FROM dual
select * from t3
Output trace Reference trace_element
BSC/US/0003/0008 BSC/UTC/0015/0003
Now the final output what i want is SR_TRC | BR_TRC | US_TRC | UTC_TRC
BSC/SR/0001/0002 BSC/DS/0007/0013 BSC/US/0003/0008 BSC/UTC/0015/0003
BSC/SR/0001/0002 BSC/DS/0007/0013 BSC/US/0003/0008 BSC/STC/0008/0002
BSC/SR/0001/0002 BSC/DS/0007/0013 BSC/US/0003/0008 BSC/UTC/0015/0003
So kindly suggest how to acheive the above result
Hi,
Thanks for posting the sample data.
Achyut K wrote:
... Now the final output what i want is
SR_TRC | BR_TRC | US_TRC | UTC_TRC
BSC/SR/0001/0002 BSC/DS/0007/0013 BSC/US/0003/0008 BSC/UTC/0015/0003
BSC/SR/0001/0002 BSC/DS/0007/0013 BSC/US/0003/0008 BSC/STC/0008/0002
BSC/SR/0001/0002 BSC/DS/0007/0013 BSC/US/0003/0008 BSC/UTC/0015/0003 It would help if you formatted the output. It's hard to tell if you want 1 column of output or 4 columns.
If rows in t have children, those children will be in t1, but if rows in t1 have children, those children may either be in t1 or in t3; is that right?
If so, do a CONNECT BY query on the UNION of t1 and t3, to get most of the columns; then join to t to get column st_trc, like this:
WITH combined_data AS
SELECT trace_reference
, trace_element
FROM t1
UNION ALL
SELECT trace_reference
, trace_element
FROM t3
, connect_by_results AS
SELECT CONNECT_BY_ROOT trace_reference AS br_trc
, CONNECT_BY_ROOT trace_element AS us_trc
, trace_element AS utc_trc
FROM combined_data
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY trace_reference = PRIOR trace_element
SELECT t.trace_element AS sr_trc
, cbr.*
FROM t
JOIN connect_by_results cbr ON cbr.br_trc = t.trace_element
;If children of t can be in any table, then, instead of joining to t at the end, add t to the UNION in combined_results. In that case, you can add a START WITH clause, and you may need SYS_CONNECT_BY_PATH, depending on your exact requirements.
Similar Messages
-
Hi
I have a cube with a dimension that has a value based hierarchy (id, description,parent)
Is it possible to query the cube to get parent child sets ?
I want the result set to look like
Member Value Parent
Parent_1 Measure_Value NULL
Child_1.1 Measure_Value Parent_1
Child_1.2 Measure_Value Parent_1
Parent_2 Measure_Value NULL
Child_2.1 Measure_Value Parent_2
Child_2.2 Measure_Value Parent_2
Also I am using a java front end,so I would like to do some pagination
So I would like say 1st 50 sets , 2nd 50 sets and so onCube Materialized Views are only created if you enable them and they are also only compatible with level based hierarchies. So MVs are not appropriate in your case.
If you are generating your own SQL then you could consider a hybrid approach by combining OLAP DML and SQL to get the explicit list of members. First execute the following OLAP DML.
LIMIT my_dim TO 'Parent_1' 'Parent_2'
LIMIT my_dim ADD DESCENDANTS USING my_dim_parentrel
SORT my_dim HIERARCHY my_dim_parentrelNow get the dimension members via either OLAP DML (e.g. REPORT command) or running SQL after the limit above. This gives you the explicit list of members that you can feed into a subsequent query.
SELECT dim_key, ...
FROM my_dim_view, cube_view
WHERE
<join condition>
AND dim_key IN (...list from report command...}
Make sure you set status back via ALLSTAT after getting the list or it will change results later. -
PAS QUERY: parent-child in to different columns from single dimension
Hello,
We are looking for the syntax to execute a PAS (Pilot Application Server) query with the following characteristics:
1) Products dimension is hierarchically arranged by Product Type (parent) and the Product itself (child). There are other ancestors but we wonu2019t necessarily use them.
2) We want to obtain a report through PAS scripting which allows us to display product hierarchy names in two different columns, i.e.: Column1 (Product Type) Column2 (Product) , but since they are part of the same hierarchy (dimension), we are not able to present the results as shown in the example below:
Example:
Product_Type Product Metric
Core ProductX MetricN
Core ProductY MetricN
New ProductW MetricN
New ProductZ MetricN
Any clues on how to attain this?
Thanks in advance.Hi,
You can make use of CrossJoin function in MDX, I give a sample MDX based on the AdventureWorks database:
select {[Measures].[Reseller Sales Amount]} on 0,
crossjoin({[Sales Territory].[Sales Territory Group].[Sales Territory Group].members},{[Sales Territory].[Sales Territory Country].[Sales Territory Country].members}) on 1
from [Adventure Works]
After execut the MDX you will get the result like this:
Reseller Sales Amount
Europe
France
$4,607,537.94
Europe
Germany
$1,983,988.04
Europe
United Kingdom
$4,279,008.83
NA
NA
(null)
North America
Canada
$14,377,925.60
North America
United States
$53,607,801.21
Pacific
Australia
$1,594,335.38
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Query Help with Parent, Child, Child's Child
Hi all,
Need some help with a query. I'm trying to create a stored procedure that is sort of like a Customer, Order, Order, Details. In my situation the tables are different but nevertheless, I want to grab all the fields from the Parent, Child,
and Childs' Child, where the Parent.ParentID = @Parameter. I tried this:
CREATE PROCEDURE [dbo].[spGetCompleteProjectXML]
@ProjectID int = 0
AS
SELECT *,
(SELECT *,
(SELECT *
FROM PageControls
WHERE (PageControls.ProjectPageID = ProjectPages.ProjectPageID))
FROM ProjectPages
WHERE (ProjectPages.ProjectID = @ProjectID))
FROM Projects
WHERE (ProjectID = @ProjectID)
FOR XML AUTO, ELEMENTS
RETURN 0
I think I'm close, but it was my best effort. Could someone help?
thanks in advanceHi TPolo,
Regarding your description, are you looking for a sample like below?
CREATE TABLE customer(customerID INT, name VARCHAR(99))
INSERT INTO customer VALUES(1,'Eric')
INSERT INTO customer VALUES(2,'Nelson')
CREATE TABLE orders(orderID INT,customerID INT)
INSERT INTO orders VALUES(1,1);
INSERT INTO orders VALUES(2,1)
INSERT INTO orders VALUES(3,2)
INSERT INTO orders VALUES(4,2)
CREATE TABLE orderDetails(orderID INT,item VARCHAR(99))
INSERT INTO orderDetails VALUES(1,'APPLE1')
INSERT INTO orderDetails VALUES(1,'BANANA1')
INSERT INTO orderDetails VALUES(2,'APPLE2')
INSERT INTO orderDetails VALUES(2,'BANANA2')
INSERT INTO orderDetails VALUES(3,'APPLE3')
INSERT INTO orderDetails VALUES(3,'BANANA3')
INSERT INTO orderDetails VALUES(4,'APPLE4')
INSERT INTO orderDetails VALUES(4,'BANANA5')
SELECT customer.customerID,customer.name,
(SELECT orderId,
SELECT item FROM orderDetails WHERE orderID=orders.orderID FOR XML AUTO,TYPE,ELEMENTS
FROM orders Where customerID=customer.customerID FOR XML AUTO,TYPE,ELEMENTS)
FROM customer WHERE customerID=1
FOR XML AUTO,ELEMENTS
DROP TABLE customer,orderDetails,orders
If you have any feedback on our support, please click
here.
Eric Zhang
TechNet Community Support -
Parent child relation using query
Hi,
If i pass one parameter as a parent.It will
retrieve all the child and subchild...
but it will be retrieved by one single query.
Is it possible?If possible pls give me an idea.
I got the inner most child using stored procedure and recursive fn.
But I couldnt get the child and subchild using
single query....
pls give an idea
below i gave table for example
Sno Parent Child
1 MD GM
2 GM Material manager
3 GM Production manager
4 Production manager Material staff
5 Material staff production staff
6 production staff Material student
7 Material student production student
if i pass GM as a parent. it will retrieve
material manager and production manager...but it will be worked in single query
ThanksHi,
SELECT * FROM t11
START WITH parent='GM'
CONNECT BY PRIOR child=parent
Out put :-
2 GM Material manager
3 GM Production manager
4 Production manager Material staff
5 Material staff production staff
6 production staff Material student
7 Material student production student
Pavan Kumar N -
Incorrect query forms in parent child relation
Hello
I am using jdev of RUP3 (Release 4) which is 11.1.1.6.2 and have following scenario
I have a ParentVO (ProfileVO) and a child VO(CreditReceiversVO) (child VO is basically extended from another VO not in this application and included as a jar library).
There a VL between ParentVO and ChildVO 1:* with two attributes ord_id and rule_id
On UI these two VO instances are dropped as parent child and I get exception on running page in logs saying RuleAssignementEO.ORG_ID invalid identifier.
Looking into the query formed by BC4J I see it as below which is clearly wrong as the alias RuleAssignmentEO is not visible for the outermost where clause. Please advise
"SELECT count(1) FROM (SELECT * FROM (SELECT /*+ FIRST_ROWS(10) */
RuleAssignmentsEO.RULE_ASSIGNMENT_ID,
RuleAssignmentsEO.OBJECT_VERSION_NUMBER,
RuleAssignmentsEO.RULE_ID,
RuleAssignmentsEO.ASSIGNED_OBJECT_ID,
RuleAssignmentsEO.ASSIGNED_OBJECT_TYPE,
RuleAssignmentsEO.ORG_ID,
RuleAssignmentsEO.ROLE_ID,
RuleAssignmentsEO.START_DATE,
RuleAssignmentsEO.END_DATE,
RuleAssignmentsEO.SPLIT_PCT,
RuleAssignmentsEO.REVENUE_TYPE,
RuleAssignmentsEO.ROLLUP_FLAG,
RuleAssignmentsEO.CREATED_BY,
RuleAssignmentsEO.CREATION_DATE,
RuleAssignmentsEO.LAST_UPDATED_BY,
RuleAssignmentsEO.LAST_UPDATE_DATE,
RuleAssignmentsEO.LAST_UPDATE_LOGIN,
RuleAssignmentsEO.ATTRIBUTE_CATEGORY,
RuleAssignmentsEO.ATTRIBUTE1,
RuleAssignmentsEO.ATTRIBUTE2,
RuleAssignmentsEO.ATTRIBUTE3,
RuleAssignmentsEO.ATTRIBUTE4,
RuleAssignmentsEO.ATTRIBUTE5,
RuleAssignmentsEO.ATTRIBUTE6,
RuleAssignmentsEO.ATTRIBUTE7,
RuleAssignmentsEO.ATTRIBUTE8,
RuleAssignmentsEO.ATTRIBUTE9,
RuleAssignmentsEO.ATTRIBUTE10,
RuleAssignmentsEO.ATTRIBUTE11,
RuleAssignmentsEO.ATTRIBUTE12,
RuleAssignmentsEO.ATTRIBUTE13,
RuleAssignmentsEO.ATTRIBUTE14,
RuleAssignmentsEO.ATTRIBUTE15,
RuleAssignmentsEO.GROUP_MEMBER_CREDIT_FLAG,
RuleAssignmentsEO.SUMMARY_FLAG,
ParticipantEO.PARTICIPANT_NAME,
ParticipantEO.PARTICIPANT_ID
FROM CN_RS_RULE_ASSIGNMENTS_ALL RuleAssignmentsEO, CN_SRP_PARTICIPANT_HDR_RO_V ParticipantEO
WHERE RuleAssignmentsEO.ASSIGNED_OBJECT_ID = ParticipantEO.PARTICIPANT_ID(+)) QRSLT WHERE RuleAssignmentsEO.RULE_ID = :Bind_RuleId AND RuleAssignmentsEO.ORG_ID = :Bind_OrgId) "
Edited by: Vik2 on Oct 23, 2012 11:30 AMinvalid identifier means ,it representating that field currently not available in db-table.
so please refer db-table. whether field avialable or not?.
edited lately:
djbo.debugoutput=console did you use this :) it will say the query is send to server.
Edited by: user707 on Oct 22, 2012 11:58 PM -
Parent Child Relationship in the table to be queried.
Hi guys,
I have the below query.
Table 1:
Column_name Reference
Value1 Value1
Value2 Value1
Table 2:
Column_name Attributes(child Elements)
Value1 Child1
Value1 Child2
Now my query is to retrieve records as below using both the tables.
Value1
Value1.Child1
Value1.Child2
Value2
Value2.Child1
Value2.Child2
How can I get such ?
Regards,
SQL Developer.Hi,
As Alex said, you should have tried by joining them.
With Parent As (
select 'Value1' p_col1,'Value1' P_col2 from dual union all
select 'Value2','Value1' from dual),
child as (
select 'Value1' c_col1,'Child1' c_col2 from dual union all
select 'Value1','Child2' from dual)
-- End of sample data
select p_col1,null c_col2 from parent
union
select p_col1,c_col2 from parent,child
where p_col2=c_col1
order by p_col1,c_col2 desc
PRAZY@11gR1> /
P_COL1 C_COL2
Value1
Value1 Child2
Value1 Child1
Value2
Value2 Child2
Value2 Child1
6 rows selected.
Elapsed: 00:00:00.03Regards,
Prazy -
Parent-Child querying in Web Service 2.0
Hello,
Our team has just started to work on the integration from ws 1.0 to ws 2.0. One tuff issue that we meet and we are searching for help is:
Since from ws 2.0 all objects are exposed at the same level and the attachements child objects are not exposed (compared to the ws 1.0 which attach childs along ListOfXXX with parents), we are wondering how then to querying these child objects now ? More explicitly, with knowing the parent object, how to retrieve its child objects ? Or it becomes impossible since ws 2.0 ?
This issue is really confusing us now, and we did not find the answer from the new "Oracle Web Service On Demand Guilde v4.0".
Any help/answer/comment/suggestion on this issue would be greatly appreciate !
FangHi all,
Thanks for your replies. Well at this point, i must say it's really a dispointment for the 2.0 release which is supposed to (at least seems suppose to) replace the previous one.
Could you please give us an idea that is the query by chilid feature will be implement in future release and if it is when ?
Thanks again for your clarifications.
Fang -
Except function with parent child hierarchy using mdx query
HI,
I need to remove some items using mdx query in parent child hierarchy.
I tried with below query,it is not removeing ,could you please help me.
SELECT
{[Measures].[Amount]} ON
COLUMNS
NonEmpty([Account].[Accounts].[Account Level 01],[Measures].[Amount])
ON ROWS
FROM (SELECT
-Descendants({[Account].[Accounts].&[47]},,Leaves)
} ON COLUMNS
FROM [Adventure Works])
induHi Indu,
Why do you remove some itmes using
Descendants function? Generally, we use FILTER function to achieve the requirement.
WITH SET [CustomAccounts] AS
FILTER ([Account].[Accounts].[Account Level 01].Members , [Account].[Accounts].CurrentMember.Name<>'Net Income')
SELECT {[Measures].[Internet Sales Amount]} ON 0
[CustomAccounts] ON 1
FROM [Adventure Works]
WITH SET [CustomAccounts] AS
FILTER ([Account].[Accounts].[Account Level 01].Members , [Account].[Accounts].CurrentMember.Name='Net Income')
SET [RemainingAccount] AS
[Account].[Accounts].[Account Level 01]- [CustomAccounts]
SELECT {[Measures].[Reseller Sales Amount]} ON 0
[RemainingAccount] ON 1
FROM [Adventure Works]
Results.
References.
http://www.databasejournal.com/features/mssql/article.php/3306101/MDX-Essentials-Basic-Set-Functions-The-Filter-Function.htm
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c0149678-97f2-4066-903f-7f9e9ff09b56/filtering-out-dimension-members-with-mdx?forum=sqlanalysisservices
Regards,
Charlie Liao
If you have any feedback on our support,
please click here.
Charlie Liao
TechNet Community Support -
[OIM] - Form Complex Lookup Query on child form
Hi all expert
I have two form, which is parent-child relationship.
I need to form a lookup query on child form's lookupfield to let user select the value but lookup query depens on the parent form on what user selecting..
Example as:
Parent form - Country Code - 100
Child Form - Department Code - Lookup query will be = Select DepartmentCode From DeparmentLookup where country ="100"
The "100" value depends on what user choice on CountryCode fro mparent form.
Can i do something like this ?
Thank in advanced
JohnThis happen for a property setting in the relation. try this..
Select all your relation in the form under Relation nodes (data block >> relation)
go to property or press F4 and set the following property to YES
Prevent Masterless Operations = Yescompile and check..
Hope this will works.. ;)
Hamid
Mark correct/helpful to help others to get right answer(s).* -
Retrieve Parent Child Sub-Child recors in SQL Query
I've a self reference table and it has Parent-Child-Subchild relation. I'm getting an output (from second answer http://stackoverflow.com/questions/16031000/get-records-containing-all-the-child-records-in-sql-server) as expected, but it's
displaying results in below format.
<parent id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<sub child id> - <name> - <code>
<sub child id> - <name> - <code>
<sub child id> - <name> - <code>
I want to display the results like in below format with applying some indent style to distinguish parent\ child\sub-child relationship.
<parent id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<sub child id> - <name> - <code>
<sub child id> - <name> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
<sub child id> - <label> - <code>
<child id> - <name> - <code>
<child id> - <name> - <code>
Please assist me on foumulating results as expected.BOL has a good example of doing this in the CTE topic. See http://technet.microsoft.com/en-us/library/ms175972%28v=sql.105%29.aspx Here is a slightly modified
version of what is in BOL.
CREATE TABLE #MyEmployees
EmployeeID smallint NOT NULL,
FirstName nvarchar(30) NOT NULL,
LastName nvarchar(40) NOT NULL,
Title nvarchar(50) NOT NULL,
DeptID smallint NOT NULL,
ManagerID int NULL,
CONSTRAINT PK_EmployeeID PRIMARY KEY CLUSTERED (EmployeeID ASC)
-- Populate the table with values.
INSERT INTO #MyEmployees VALUES
(1, N'Ken', N'Sánchez', N'Chief Executive Officer',16,NULL)
,(273, N'Brian', N'Welcker', N'Vice President of Sales',3,1)
,(274, N'Stephen', N'Jiang', N'North American Sales Manager',3,273)
,(275, N'Michael', N'Blythe', N'Sales Representative',3,274)
,(276, N'Linda', N'Mitchell', N'Sales Representative',3,274)
,(285, N'Syed', N'Abbas', N'Pacific Sales Manager',3,273)
,(286, N'Lynn', N'Tsoflias', N'Sales Representative',3,285)
,(16, N'David',N'Bradley', N'Marketing Manager', 4, 273)
,(23, N'Mary', N'Gibson', N'Marketing Specialist', 4, 16);
; WITH DirectReports(Name, Title, EmployeeID, EmployeeLevel, Sort)
AS (SELECT CONVERT(varchar(255), e.FirstName + ' ' + e.LastName),
e.Title,
e.EmployeeID,
1,
CONVERT(varchar(255), e.FirstName + ' ' + e.LastName)
FROM #MyEmployees AS e
WHERE e.ManagerID IS NULL
UNION ALL
SELECT CONVERT(varchar(255), REPLICATE (' ' , EmployeeLevel) +
e.FirstName + ' ' + e.LastName),
e.Title,
e.EmployeeID,
EmployeeLevel + 1,
CONVERT (varchar(255), RTRIM(Sort) + '| ' + FirstName + ' ' +
LastName)
FROM #MyEmployees AS e
JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID
SELECT EmployeeID, Name, Title, EmployeeLevel
FROM DirectReports
ORDER BY Sort;
GO
drop table #MyEmployees;
Tom -
Hi All,
I have a requirement to pull parent child partynames from hz_parties,hz_relationships table
I need output like below:
Partyname, partynumber,immediateparent(partyname),globlapartyname..
could you please suggest me.I am new to EBS..
Thanks
BhavanaAny Help please....
Edited by: user13689917 on Mar 9, 2011 2:32 PM
Edited by: user13689917 on Mar 9, 2011 2:33 PM -
The Parent-Child Hierarchy can't be queried in any intermediate node
Hi All,
I've created a Parent-Child Hierarchy with Obiee 11g(11.1.1.3).
I can use the filter to select any Root-Node and it can be expanded correctly.But if I choose any intermediate node,it can't be expanded.Then what can I do?
Thanks in advance!
Edited by: Needn on 2011-9-8 下午8:42Thx for your advice,but my qusetion has not been solved.
I don't know how to use these function in the analytics page.Must I modify its default SQL and apply mine?
Its default SQL like the following ,then how can I modify it to achieve my purpose?
SELECT s_0, s_1, s_2, s_3, s_4 FROM (
SELECT
*0 s_0,*
*"model"."DIM_PROD_COST_CATEGORY"."CATEGORY_NAME" s_1,*
CASE WHEN ISLEAF("model"."DIM_PROD_COST_CATEGORY"."DIM_PROD_COST_CATEGORYDim") THEN 1 ELSE 0 END s_2,
IDOF("model"."DIM_PROD_COST_CATEGORY"."DIM_PROD_COST_CATEGORYDim") s_3,
PARENT("model"."DIM_PROD_COST_CATEGORY"."DIM_PROD_COST_CATEGORYDim") s_4
FROM "model"
WHERE
ISROOT("model"."DIM_PROD_COST_CATEGORY"."DIM_PROD_COST_CATEGORYDim")
*) djm ORDER BY 1* -
Help needed in retrieving parent child relationship values
Hi,
I have a requirement to get parent child relationship values as below.
Ex: Address table
cont cont_code state state_code
C1 10 S1 1
C1 10 S2 2
C1 10 S3 3
C2 20 S4 4
C2 20 S5 5
C3 30 S6 6
C3 30 S7 7
C3 30 S8 8
I want a result of country/state and corresponding code like below.
corresponding states should be displayed under each country with some space appended to it.
Geography code
C1 10
S1 1
S2 2
S3 3
C2 20
S4 4
S5 5
C3 30
S6 6
S7 7
S8 8
I am using oracle 10g version.
Thanks in advance.Hi,
When you post formatted text (like your output) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
I think you're saying that you want this output:GEOGRAPHY CODE
C1 10
S1 1
S2 2
S3 3
C2 20
S4 4
S5 5
C3 30
S6 6
S7 7
S8 8
If so, UNION, as Hoek suggested, is a good way.
GROUP BY ROLLUP is more efficient, but harder to understand:SELECT CASE
WHEN GROUPING (state) = 1
THEN cont
ELSE ' ' || state
END AS geography
, CASE
WHEN GROUPING (state) = 1
THEN MAX (cont_code)
ELSE MAX (state_code)
END AS code
FROM t
GROUP BY cont
, ROLLUP (state)
ORDER BY cont
, state NULLS FIRST
By the way, this looks like a bad table design.
In a relational database, the fact that the name 'C1' belongs to cont_code 10 should only be stored in one place. You have the same information on 3 separate rows.
Also, if 'C1' and 'S1' are both names, they should probably be in the same column, so that (to give just one example) you can find the information about 'x1' without knowing if it is a cont or a state.
A better design would be.NAME CODE PARENT DSCR
==== ==== ====== ====
C1 10 CONT
S1 1 10 STATE
S2 2 10 STATE
S3 3 10 STATE
C2 20 CONT
S4 4 20 STATE
S5 5 20 STATE
C3 30 CONT
S6 6 30 STATE
S7 7 30 STATE
S8 8 30 STATE
If the data is this simple, then the dscr column isn't needed. Whether parent is NULL or not tells whether the rows represents a cont or a state.
To get the results you want from a table like this, you could use CONNECT BY. Using either UNION or ROLLUP, you have to know, at the time you write the query, how many levels there will be in the parent-child tree, and the length of the code is proportional to that depth, and the table has to be changed if you ever need to add another level. CONNECT BY can handle any number of levels, and the length and complexity of the code is the same whether you have just 2 levels (countries and strates), or 7 levels (continents, regions, countiries, states, districts, cities and neighborhoods) or 72 levels. The table doesn't need any more columns, no matter how deep the tree gets.
Edited by: Frank Kulash on Sep 16, 2010 11:54 AM -
Getting parent-child data from a single column in a table
Hi,
I have a parent-child data in a column.
Eg:
0
00
01
010
011
1
10
11
These values are present in the single column itself. My need is to get the parent values for the given child value.
For eg: If I am giving the input as 011, the query should return all its parents, i.e. 01 and 0
Could you please give me any inputs on this?
Thanks,
GVFrank Kulash wrote:
Assuming that each child's id is formed by adding exactly one character to the end of its parent's id:
SELECT id
FROM table_x
START WITH id = :target_id
CONNECT BY PRIOR id LIKE id || '_';
Small tweak to yours Frank if it's just the parents that need identifying...
SQL> ed
Wrote file afiedt.buf
1 with t as (select '0' as x from dual union all
2 select '00' from dual union all
3 select '01' from dual union all
4 select '010' from dual union all
5 select '011' from dual union all
6 select '1' from dual union all
7 select '10' from dual union all
8 select '11' from dual)
9 -- end of test data
10 SELECT x
11 FROM t, (select '&required' as req from dual) req
12 WHERE x != req.req
13 START WITH x = req.req
14* CONNECT BY PRIOR x LIKE x || '_'
SQL> /
Enter value for required: 011
old 11: FROM t, (select '&required' as req from dual) req
new 11: FROM t, (select '011' as req from dual) req
X
01
0
SQL> /
Enter value for required: 11
old 11: FROM t, (select '&required' as req from dual) req
new 11: FROM t, (select '11' as req from dual) req
X
1
SQL>
Maybe you are looking for
-
F4 help for a field in alv grid
hello friends, i want to make one of the fields of my alv grid as editable and i want f4 help option also available for it, can v do it, if yes how??? pls explain through code thanks
-
LE8 crashes when trying to open Gearbox Plugin
I am having a problem where every time I try to open the Gearbox plugin in LE8 the program will crash. The only way I have been able to record is to open Gearbox first, establish a guitar tone, and then subsequently open LE8 and the tones will work a
-
Sap database tables with adobe forms
Hai all, I am new to adobe forms...Can anyone explain me how to link sap database with adobe forms.... Any documents regarding tht will be helpful..... Thanks in advance....
-
RE: ActiveX control and Active Server Pages?
Martin, if it works in standalone VB program but not in an .asp page through VBScript, I guess the reason may be the type mismatch on the parameters of the ActiveX automation methods. VBScript is a subset of VB and only supports the OLE data type VAR
-
Intermittent JNI Panic exception when accessing Oracle
I was wondering if anyone had seem this error before and if so, did you find a resolution. The system is a Solaris system running oracle 8.0.6 using the JDBC 9.0.1 thin client drivers. Any help would be greatly appriciated. The error as reported is:-