473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Oracle Query

This is a treeview

Root
-- Cricket1
---Cricket2
---sachin
--Cricket3
--dravid

--FootBALL1
--pele


I have a table like this

Id type name
100 Root Root
101 cricket Cricket1
102 cricket Cricket2
103 player sachin
104 cricket Cricket3
105 player dravid
106 football FootBALL1
107 player pele

To maintain the parent-child relationship i have the following
relation table
id ParentId

100 null
101 100
102 101
103 102
104 100
105 104
106 100
107 106

I need to query the db and the get the following result

Root
-- Cricket1
---Cricket2

--Cricket3
--FootBALL1
--pele

means when ever it encounters the type of cricket it should not get
the childs inside it(as in Cricket3) however if it encounters a child
of type cricket , it should go ahead and get the child (as in
Cricket1)

Its oracle db , so start with connect by clause can be used

Thanks in Advance

sajid
Jul 19 '05 #1
4 8930

"sajid" <sa******@yahoo.com> wrote in message
news:a2*************************@posting.google.co m...
| This is a treeview
|
| Root
| -- Cricket1
| ---Cricket2
| ---sachin
| --Cricket3
| --dravid
|
| --FootBALL1
| --pele
|
|
|
|
| I have a table like this
|
| Id type name
| 100 Root Root
| 101 cricket Cricket1
| 102 cricket Cricket2
| 103 player sachin
| 104 cricket Cricket3
| 105 player dravid
| 106 football FootBALL1
| 107 player pele
|
|
|
| To maintain the parent-child relationship i have the following
| relation table
|
|
| id ParentId
|
| 100 null
| 101 100
| 102 101
| 103 102
| 104 100
| 105 104
| 106 100
| 107 106
|
|
|
| I need to query the db and the get the following result
|
|
|
| Root
| -- Cricket1
| ---Cricket2
|
| --Cricket3
|
|
| --FootBALL1
| --pele
|
|
|
| means when ever it encounters the type of cricket it should not get
| the childs inside it(as in Cricket3) however if it encounters a child
| of type cricket , it should go ahead and get the child (as in
| Cricket1)
|
| Its oracle db , so start with connect by clause can be used
|
| Thanks in Advance
|
| sajid

it looks like you have a simple 1:M hierarchy -- that should be modelled in
a single table, not two

you are correct that CONNECT BY can be used -- but you seem to imply that
you are having trouble with it and would like someone to show you how to do
it with your data.

i would suggest you try the examples in the Oracle SQL Manual (under SELECT)
until you understand how it works, then apply the technique to your own
data. if you have problems after to try it yourself, go ahead and post your
statement and errors (and db version) and no doubt you'll get plenty of help

-- mcs
Jul 19 '05 #2
"Mark C. Stock" <mcstockX@Xenquery .com> wrote in message news:<hN********************@comcast.com>...
"sajid" <sa******@yahoo.com> wrote in message
news:a2*************************@posting.google.co m...
| This is a treeview
|
| Root
| -- (Level 1) Cricket1
| ---(Level 2) Cricket2
| --- (Level 3) sachin
| --(Level1) Cricket3
| --(Level2)dravid
|
| --(Level1)FootBALL1
| --(Level2)pele
|
|
|
|
| I have a table like this
|
| Id type name
| 100 Root Root
| 101 cricket Cricket1
| 102 cricket Cricket2
| 103 player sachin
| 104 cricket Cricket3
| 105 player dravid
| 106 football FootBALL1
| 107 player pele
|
|
|
| To maintain the parent-child relationship i have the following
| relation table
|
|
| id ParentId
|
| 100 null
| 101 100
| 102 101
| 103 102
| 104 100
| 105 104
| 106 100
| 107 106
|
|
|
| I need to query the db and the get the following result
|
|
|
| Root
| -- Cricket1
| ---Cricket2
|
| --Cricket3
|
|
| --FootBALL1
| --pele
|
|
|
| means when ever it encounters the type of cricket it should not get
| the childs inside it(as in Cricket3) however if it encounters a child
| of type cricket , it should go ahead and get the child (as in
| Cricket1)
|
| Its oracle db , so start with connect by clause can be used
|
| Thanks in Advance
|
| sajid

it looks like you have a simple 1:M hierarchy -- that should be modelled in
a single table, not two

you are correct that CONNECT BY can be used -- but you seem to imply that
you are having trouble with it and would like someone to show you how to do
it with your data.

i would suggest you try the examples in the Oracle SQL Manual (under SELECT)
until you understand how it works, then apply the technique to your own
data. if you have problems after to try it yourself, go ahead and post your
statement and errors (and db version) and no doubt you'll get plenty of help

-- mcs


Dear mark
Great that u noticed it is simple 1:m hierarchy..,thanks for the
advice to model in a single table,but dear dude dont look at the table
structure, that was just a example i gave to make it simple , u should
have read the explanation properly

my question was using start with connect by clause
1.u can easily traverse thro all the childs for a particular Id
2.u can even stop at some particular type -- say stop at
cricket/football ... ok
3.my question is,it stops at the first occurence of cricket and if the
immediate child happens to be cricket again (then i need to display
that child cricket also else stop) .. In my case i am not able to
display cricket2 under cricket1

For the clarity purpose i have put the levels besides the hierarchy

ok bye
sajid
Jul 19 '05 #3
VC
Hello sajid,

Well, it's easy enough. In Oracle 9i:

drop table t1;
create table t1(Id int, typ varchar2(10), name varchar2(10));
insert into t1 values(100,'Root' ,'Root');
insert into t1 values(101,'cricket' ,'Cricket1');
insert into t1 values(102,'cricket' ,'Cricket2');
insert into t1 values(103,'player' ,'sachin');
insert into t1 values(104,'cricket' ,'Cricket3');
insert into t1 values(105,'player' ,'dravid');
insert into t1 values(106,'football','FootBALL1');
insert into t1 values(107,'player' , 'pele');
drop table t1;
create table t2 (id int, ParentId int);
insert into t2 values(100,null);
insert into t2 values(101,100);
insert into t2 values(102,101);
insert into t2 values(103,102);
insert into t2 values(104,100);
insert into t2 values(105,104);
insert into t2 values(106,100);
insert into t2 values(107,106);

select lpad('-', level-1, '-')||name name from t1 join t2 on t1.id=t2.id
connect by prior t2.id=t2.parentid and (prior typ != 'cricket' or
typ!='player')
start with t2.parentid is null;

Root
-Cricket1
--Cricket2
-Cricket3
-FootBALL1
--pele

Rgds.
"sajid" <sa******@yahoo.com> wrote in message
news:a2**************************@posting.google.c om...
"Mark C. Stock" <mcstockX@Xenquery .com> wrote in message

news:<hN********************@comcast.com>...
"sajid" <sa******@yahoo.com> wrote in message
news:a2*************************@posting.google.co m...
| This is a treeview
|
| Root
| -- (Level 1) Cricket1
| ---(Level 2) Cricket2
| --- (Level 3) sachin
| --(Level1) Cricket3
| --(Level2)dravid
|
| --(Level1)FootBALL1
| --(Level2)pele
|
|
|
|
| I have a table like this
|
| Id type name
| 100 Root Root
| 101 cricket Cricket1
| 102 cricket Cricket2
| 103 player sachin
| 104 cricket Cricket3
| 105 player dravid
| 106 football FootBALL1
| 107 player pele
|
|
|
| To maintain the parent-child relationship i have the following
| relation table
|
|
| id ParentId
|
| 100 null
| 101 100
| 102 101
| 103 102
| 104 100
| 105 104
| 106 100
| 107 106
|
|
|
| I need to query the db and the get the following result
|
|
|
| Root
| -- Cricket1
| ---Cricket2
|
| --Cricket3
|
|
| --FootBALL1
| --pele
|
|
|
| means when ever it encounters the type of cricket it should not get
| the childs inside it(as in Cricket3) however if it encounters a child
| of type cricket , it should go ahead and get the child (as in
| Cricket1)
|
| Its oracle db , so start with connect by clause can be used
|
| Thanks in Advance
|
| sajid

it looks like you have a simple 1:M hierarchy -- that should be modelled in a single table, not two

you are correct that CONNECT BY can be used -- but you seem to imply that you are having trouble with it and would like someone to show you how to do it with your data.

i would suggest you try the examples in the Oracle SQL Manual (under SELECT) until you understand how it works, then apply the technique to your own
data. if you have problems after to try it yourself, go ahead and post your statement and errors (and db version) and no doubt you'll get plenty of help
-- mcs


Dear mark
Great that u noticed it is simple 1:m hierarchy..,thanks for the
advice to model in a single table,but dear dude dont look at the table
structure, that was just a example i gave to make it simple , u should
have read the explanation properly

my question was using start with connect by clause
1.u can easily traverse thro all the childs for a particular Id
2.u can even stop at some particular type -- say stop at
cricket/football ... ok
3.my question is,it stops at the first occurence of cricket and if the
immediate child happens to be cricket again (then i need to display
that child cricket also else stop) .. In my case i am not able to
display cricket2 under cricket1

For the clarity purpose i have put the levels besides the hierarchy

ok bye
sajid

Jul 19 '05 #4
Hello VC
thanks for the answer, "prior type" was thing i was missing

rgs

sajid
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
4
by: susmita_ganguly | last post by:
Hi I am trying to upgrade from oracle 8i to oracle 9i on the same server ..I don't know much abt migration . Can anyone help me out. Thanks. Susmita
11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
1
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to...
3
by: Jon Ole Hedne | last post by:
My Access 2002-application need to work with tables from both Oracle and Access. To solve this, I want to run some querys on three views in Oracle and import the results into temporary...
8
by: Alfonso Esteban Gonzalez Sencion | last post by:
I am trying to use Access as a front end for extracting information from an Oracle database. I started using linked tables but I am getting a very curious behaviour. When I consult the linked...
4
by: robboll | last post by:
When I try to use an append query from an oracle link it takes forever. I am exploring the idea of doing an append action using a pass-through query. If I have an Oracle ODBC connection to...
5
by: jonceramic | last post by:
Hi All, I started developing in Access, and people took notice and so we're starting to migrate into our corporate's bigger Oracle system. I'll still be using my developed Access front ends,...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
2
by: Vinod Sadanandan | last post by:
All, Below listed are the new features in Oracle 11g ,please join me in this discussion to generate a testcase and analyze each of the listed features . Precompilers:...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.