473,668 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8952

"sajid" <sa******@yahoo .com> wrote in message
news:a2******** *************** **@posting.goog le.com...
| 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@Xenqu ery .com> wrote in message news:<hN******* *************@c omcast.com>...
"sajid" <sa******@yahoo .com> wrote in message
news:a2******** *************** **@posting.goog le.com...
| This is a treeview
|
| Root
| -- (Level 1) Cricket1
| ---(Level 2) Cricket2
| --- (Level 3) sachin
| --(Level1) Cricket3
| --(Level2)dravid
|
| --(Level1)FootBAL L1
| --(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..,tha nks 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,'Roo t' ,'Root');
insert into t1 values(101,'cri cket' ,'Cricket1');
insert into t1 values(102,'cri cket' ,'Cricket2');
insert into t1 values(103,'pla yer' ,'sachin');
insert into t1 values(104,'cri cket' ,'Cricket3');
insert into t1 values(105,'pla yer' ,'dravid');
insert into t1 values(106,'foo tball','FootBAL L1');
insert into t1 values(107,'pla yer' , '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.parent id 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.goo gle.com...
"Mark C. Stock" <mcstockX@Xenqu ery .com> wrote in message

news:<hN******* *************@c omcast.com>...
"sajid" <sa******@yahoo .com> wrote in message
news:a2******** *************** **@posting.goog le.com...
| This is a treeview
|
| Root
| -- (Level 1) Cricket1
| ---(Level 2) Cricket2
| --- (Level 3) sachin
| --(Level1) Cricket3
| --(Level2)dravid
|
| --(Level1)FootBAL L1
| --(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..,tha nks 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
40250
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 user commits. But in Informix there is this thing called ISOLATION LEVELS. For example by setting the ISOLATION LEVEL to DIRTY READ, a user will read dirty data, i.e. the last uncommited updated value of a field by some other user. Is
4
19986
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
12697
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 read the new date. So process B starts "select ..." but does not get the previously inserted row. The timespan between commit and select is very short. (NOTE: two different sessions are used) Questions: 1.) Does commit when returning from call...
1
2551
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 transfer to a MySQL database. No special data, constraints etc that MySQL not will handle. My solution is to reverse engineer the database from ERStudio and then produce a SQL script that will insert the data into the MySQL engine. But I can't do...
3
23452
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 Access-tables. I have tried this: conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.ConnectionString = "data source=" & datafil & ";Jet OLEDB:Database Password=" conn.Open datafil
8
4032
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 table in access, the total number of records is OK but some records appear several times and some records do not appear at all. It seems as if access or the ODBC drivers returns several times the same record and skips some of the records, curiosly...
4
7069
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 server OraTest. User: User1 Password: password and I am trying to append all records in table: tblTEST that are code: "abc"
5
7225
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, but will be migrating my back ends to Oracle ODBC. 1. Does anyone have recommendations for books or web resources for general rules/guidelines/help on doing this? I haven't found a good
7
20319
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, if they do not already exist. In terms of ASP/ADO, that would be fine in a SQL Server Sense by a simply ASP/Server-Side JavaScript as such: var cnTemp = Server.CreateObject("ADODB.Connection");
2
14230
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: Additional Array INSERT and SELECT Syntax Support by Pro*C/C++ and Pro*COBOL Precompilers: Dynamic SQL Statement Caching in Pro*C/C++ and Pro*COBOL Precompilers: Fix Execution Plan in Pro*C/C++ and Pro*COBOL Precompilers: Flexible B Area Length...
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.