473,406 Members | 2,816 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,406 software developers and data experts.

SQL trees

Hi NG,

After a break of two months I am back on my project of converting a COBOL
ERP product to C# and SQL Server. I now have to process a tree structure in
C#. Not only am I a newbie in C#, but SQL as well. So if these questions
sound stupid, please forgive me. All of my programming in C# to date, with
one exception, has been to "ask" SQL for a specific primary key. That
exception has been to fill a C# data table with all of the rows from a SQL
table. The process for the tree structure is a tad different. The tree's
primary key has 2 parts: a parent part number and a component part number.
The COBOL procedure makes use of the fact that the data is stored in an ISAM
file. By definition the index is in sequential order. It is easy to ask
COBOL for the first parent and store its component. Then see if that
component is a parent to some other component. From anywhere in the tree
you can always ask for the next sequential record and of course you can ask
for the first record. I can not figure out how to ask SQL for the first row
of a table. Further, I do not know how to ask for a row where I know the
parent, but not the component. Consider the following tree:

Parent Component
500-000 500-001
500-000 500-004
500-000 500-005
500-000 500-006
500-000 500-100
500-004 500-080
500-005 500-080
500-100 500-002
500-100 500-003

This is one tree. In the real case there would be many trees. Task 1 is to
ask SQL for the first row. In my past C# programs I would know what the
whole key is, 500-000 500-001 in this case. Now I do not know the key. How
do I ask for the first row? Next, I would want to check if the component,
500-001, is a parent (it is not). How do I ask SQL if any row begins with
500-001? The component of row 2 is in fact a parent. Asking SQL about this
component as parent should produce row 6.

My understanding of this issue would be greatly advanced if I could process
this table as a sequential file. For example I would like to ask SQL to
find the first parent that was equal to 500-100. Then I would like to ask
for the next parent that was 500-100 until it could find no more. In that
case it would find 2 rows starting at row 8.

If it would help I can supply the code I am using to get particular rows
when I know the full key to ask SQL.

Thanks in advance,

Bob
--
Robert Schuldenfrei
S. I. Inc.
32 Ridley Road
Dedham, MA 02026
bo*@s-i-inc.com
781/329-4828
Nov 16 '05 #1
6 1633

It is best that one field in a datebase table holds only one piece of
information. If your SQL table is named TREEList then there should be a
separate Compoment field and Parent field.

One way to traverse a tree is ...

To get the tree base you need to know the top parent

SELECT * FROM TREEList WHERE Component = @TopNode

To get the node's children you need to know the parent. You could order by
Component to keep the child nodes in order.

SELECT * FROM TREEList WHERE Parent = @ParentNode ORDER BY Component

Doing it this way you would setup each of these SQL statements as a
parameterised query or stored proceedure and call them through ADO.Net in
your C# application.

You could then create a Tree class that takes the parent node id and
recursively calls the children SQL statment to build the tree in your code.
Robby
"Robert Schuldenfrei" <bo*@s-i-inc.com> wrote in message
news:Cinvd.188601$V41.166389@attbi_s52...
Hi NG,

After a break of two months I am back on my project of converting a COBOL
ERP product to C# and SQL Server. I now have to process a tree structure
in C#. Not only am I a newbie in C#, but SQL as well. So if these
questions sound stupid, please forgive me. All of my programming in C# to
date, with one exception, has been to "ask" SQL for a specific primary
key. That exception has been to fill a C# data table with all of the rows
from a SQL table. The process for the tree structure is a tad different.
The tree's primary key has 2 parts: a parent part number and a component
part number. The COBOL procedure makes use of the fact that the data is
stored in an ISAM file. By definition the index is in sequential order.
It is easy to ask COBOL for the first parent and store its component.
Then see if that component is a parent to some other component. From
anywhere in the tree you can always ask for the next sequential record and
of course you can ask for the first record. I can not figure out how to
ask SQL for the first row of a table. Further, I do not know how to ask
for a row where I know the parent, but not the component. Consider the
following tree:

Parent Component
500-000 500-001
500-000 500-004
500-000 500-005
500-000 500-006
500-000 500-100
500-004 500-080
500-005 500-080
500-100 500-002
500-100 500-003

This is one tree. In the real case there would be many trees. Task 1 is
to ask SQL for the first row. In my past C# programs I would know what
the whole key is, 500-000 500-001 in this case. Now I do not know the
key. How do I ask for the first row? Next, I would want to check if the
component, 500-001, is a parent (it is not). How do I ask SQL if any row
begins with 500-001? The component of row 2 is in fact a parent. Asking
SQL about this component as parent should produce row 6.

My understanding of this issue would be greatly advanced if I could
process this table as a sequential file. For example I would like to ask
SQL to find the first parent that was equal to 500-100. Then I would like
to ask for the next parent that was 500-100 until it could find no more.
In that case it would find 2 rows starting at row 8.

If it would help I can supply the code I am using to get particular rows
when I know the full key to ask SQL.

Thanks in advance,

Bob
--
Robert Schuldenfrei
S. I. Inc.
32 Ridley Road
Dedham, MA 02026
bo*@s-i-inc.com
781/329-4828

Nov 16 '05 #2

"Robert Schuldenfrei" <bo*@s-i-inc.com> wrote in message
news:Cinvd.188601$V41.166389@attbi_s52...
Task 1 is to ask SQL for the first row. In my past C# programs I would
know what the whole key is, 500-000 500-001 in this case. Now I do not
know the key. How do I ask for the first row? Next, I would want to
check if the component, 500-001, is a parent (it is not). How do I ask
SQL if any row begins with 500-001?
To get the first parent / component combo, do this:

SELECT TOP 1 Parent,Component FROM MyTable ORDER BY Parent,Component

To find out if the first component is a parent or not (I'm assuming you are
parameterizing your queries and have place the key of the Component above
into a parameter called @Parent):

SELECT COUNT(*) AS Qty FROM MyTable WHERE Parent = @Parent
My understanding of this issue would be greatly advanced if I could
process this table as a sequential file. For example I would like to ask
SQL to find the first parent that was equal to 500-100. Then I would like
to ask for the next parent that was 500-100 until it could find no more.


One approach would be:

SELECT DISTINCT Parent FROM MyTable ORDER BY Parent

This would give you a list of all the parents keys. If you are going to
process it sequentially, forward-only, I'd use a DataReader to walk through
this list:

using (myReader) {

while (myReader.Read()) {
string parent = myReader.GetString(0);
// set up a SqlCommand and parameter here to get the Components for
the current "parent".
// then use a 2nd DataReader in an inner loop to process each
component.
}

}

If you need to be able to jump backwards in either list, use a DataTable
instead of a DataReader. But it doesn't sound like you need that
functionality, in which case, a DataReader is faster.

The above code is skelatal and incomplete but give the general idea.

--Bob
Nov 16 '05 #3
Joe Celko wrote two books "SQL For Smarties" and "Trees and Hierarchies in
SQL" that go into great detail concerning implementation of hierarchical
data structures in SQL. He provides several working examples including the
ERP-style "parts explosion". Well worth the investment.
"Robert Schuldenfrei" <bo*@s-i-inc.com> wrote in message
news:Cinvd.188601$V41.166389@attbi_s52...
Hi NG,

After a break of two months I am back on my project of converting a COBOL
ERP product to C# and SQL Server. I now have to process a tree structure
in C#. Not only am I a newbie in C#, but SQL as well. So if these
questions sound stupid, please forgive me. All of my programming in C# to
date, with one exception, has been to "ask" SQL for a specific primary
key. That exception has been to fill a C# data table with all of the rows
from a SQL table. The process for the tree structure is a tad different.
The tree's primary key has 2 parts: a parent part number and a component
part number. The COBOL procedure makes use of the fact that the data is
stored in an ISAM file. By definition the index is in sequential order.
It is easy to ask COBOL for the first parent and store its component.
Then see if that component is a parent to some other component. From
anywhere in the tree you can always ask for the next sequential record and
of course you can ask for the first record. I can not figure out how to
ask SQL for the first row of a table. Further, I do not know how to ask
for a row where I know the parent, but not the component. Consider the
following tree:

Parent Component
500-000 500-001
500-000 500-004
500-000 500-005
500-000 500-006
500-000 500-100
500-004 500-080
500-005 500-080
500-100 500-002
500-100 500-003

This is one tree. In the real case there would be many trees. Task 1 is
to ask SQL for the first row. In my past C# programs I would know what
the whole key is, 500-000 500-001 in this case. Now I do not know the
key. How do I ask for the first row? Next, I would want to check if the
component, 500-001, is a parent (it is not). How do I ask SQL if any row
begins with 500-001? The component of row 2 is in fact a parent. Asking
SQL about this component as parent should produce row 6.

My understanding of this issue would be greatly advanced if I could
process this table as a sequential file. For example I would like to ask
SQL to find the first parent that was equal to 500-100. Then I would like
to ask for the next parent that was 500-100 until it could find no more.
In that case it would find 2 rows starting at row 8.

If it would help I can supply the code I am using to get particular rows
when I know the full key to ask SQL.

Thanks in advance,

Bob
--
Robert Schuldenfrei
S. I. Inc.
32 Ridley Road
Dedham, MA 02026
bo*@s-i-inc.com
781/329-4828

Nov 16 '05 #4
Hi Robby,

Thank you for your help. Sadly, you put your finger on the nub of the
issue: "You need to know the top parent." In an ISAM file you always know
the top parent. In addition, Bills of Materials (BOM) store many trees that
each have a top parent. If memory were not an issue, I would ask SQL for
the whole BOM table and be done with it. Alas, in real world systems, these
tables have hundreds of thousands of rows. The good news seems to be that
there are two books by Joe Celko that Michael C# suggests will solve my
problem. I am about to order them.

Sincerely,

Bob
--
Robert Schuldenfrei
S. I. Inc.
32 Ridley Road
Dedham, MA 02026
bo*@s-i-inc.com
781/329-4828
"Robby" <ed****@not.my.email.com> wrote in message
news:e9**************@TK2MSFTNGP14.phx.gbl...

It is best that one field in a datebase table holds only one piece of
information. If your SQL table is named TREEList then there should be a
separate Compoment field and Parent field.

One way to traverse a tree is ...

To get the tree base you need to know the top parent

SELECT * FROM TREEList WHERE Component = @TopNode


Nov 16 '05 #5
Hi Bob,

Thank you for dealing with my Bill of Material (BOM) problem. This looks
like it will address my concerns. A bit later on today, or maybe tomorrow,
I will write some code to investigate these techniques. Certainly, SELECT
TOP 1 Parent... looks like the answer to getting started. I am a tad afraid
of doing anything like SELECT DISTINCT Parent... as real world applications
could return thousands of rows. I have been told by Michael C# that there
are two books by Joe Celko that address my problem. I am ordering them
today.

Sincerely,

Bob
--
Robert Schuldenfrei
S. I. Inc.
32 Ridley Road
Dedham, MA 02026
bo*@s-i-inc.com
781/329-4828
"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl...

"Robert Schuldenfrei" <bo*@s-i-inc.com> wrote in message
news:Cinvd.188601$V41.166389@attbi_s52...
Task 1 is to ask SQL for the first row. In my past C# programs I would
know what the whole key is, 500-000 500-001 in this case. Now I do not
know the key. How do I ask for the first row? Next, I would want to
check if the component, 500-001, is a parent (it is not). How do I ask
SQL if any row begins with 500-001?


To get the first parent / component combo, do this:

SELECT TOP 1 Parent,Component FROM MyTable ORDER BY Parent,Component

To find out if the first component is a parent or not (I'm assuming you
are parameterizing your queries and have place the key of the Component
above into a parameter called @Parent):

SELECT COUNT(*) AS Qty FROM MyTable WHERE Parent = @Parent
My understanding of this issue would be greatly advanced if I could
process this table as a sequential file. For example I would like to ask
SQL to find the first parent that was equal to 500-100. Then I would like
to ask for the next parent that was 500-100 until it could find no more.


One approach would be:

SELECT DISTINCT Parent FROM MyTable ORDER BY Parent

This would give you a list of all the parents keys. If you are going to
process it sequentially, forward-only, I'd use a DataReader to walk
through this list:

using (myReader) {

while (myReader.Read()) {
string parent = myReader.GetString(0);
// set up a SqlCommand and parameter here to get the Components for
the current "parent".
// then use a 2nd DataReader in an inner loop to process each
component.
}

}

If you need to be able to jump backwards in either list, use a DataTable
instead of a DataReader. But it doesn't sound like you need that
functionality, in which case, a DataReader is faster.

The above code is skelatal and incomplete but give the general idea.

--Bob

Nov 16 '05 #6
Hi Michael,

Thanks for the tip. I am going to order these books today. From a brief
Google search I am quite sure that this will more than handle my problem.

Sincerely,

Bob
--
Robert Schuldenfrei
S. I. Inc.
32 Ridley Road
Dedham, MA 02026
bo*@s-i-inc.com
781/329-4828
"Michael C#" <xy*@abcdef.com> wrote in message
news:mq***************@fe08.lga...
Joe Celko wrote two books "SQL For Smarties" and "Trees and Hierarchies in
SQL" that go into great detail concerning implementation of hierarchical
data structures in SQL. He provides several working examples including
the ERP-style "parts explosion". Well worth the investment.


Nov 16 '05 #7

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

Similar topics

6
by: C++ Shark | last post by:
Hi, which stl class is good for creating search trees? I am looking for something flexible, that allows me to search for a required state (of matrices, graphs, etc.) quickly. thanks in...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
2
by: trusiki | last post by:
I am trying to use C# for my program that deals with manipulation of trees (i.e. finding distance between different nodes, assigning labels to nodes, storing trees, etc.). I know I can probably...
17
Ganon11
by: Ganon11 | last post by:
Hey guys, OK, taking care of this beforehand; I AM a student in a university. This IS part of my homework, and (as a moderator), I'm doing my best to follow the posting guidelines I work so hard...
2
by: parasuram | last post by:
Hi friends ............. this is a question regarding the data structures trees Pleas post it if possible with in 2 days I will thankful if some body could help doing this. Operating...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
8
by: Bert | last post by:
How can my response to this problem be better? The pot doesn't work that well when you enter 25, 21, 17 and some others. The problem: CHRISTMAS TREES The grade 5 class at the local primary...
6
by: rsprawls | last post by:
I found a disk for a b-tree algorithm that I purchased back in 93 or so. I'd hoped to find this, but now I'd like to know how worthwhile are b-trees in today's advancements? This is old C code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
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
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.