Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2009, 08:13 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Count Left Nodes

Hello Friends,

I've a table as given below

ID LChild RChild
1 2 3
2 4 6
3 5 7
4 8 9
5 10 11
6 12 13
7 0 14


Now i've to find out the LChild of 1, 1 has 2 in it's left and 2 has 4,6 as it's child and 4 and 6 has also two-two child each......At final 1 has 7 child on it's left and on it's Right it's 6

And also i've find out for each ID i.e 1,2,3,4,5,6,7
 
Old April 7th, 2009, 03:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

This is a tough thing to do in T-SQL in SQL Server 2000 (and maybe 2005?? don't remember) because you need to use recursion, and 2000 doesn't support recursion.

SQL Server 2008 supports recursion, so it should be possible there.

What DB are you using?? 2000? 2005? 2008?

Oh...and if you don't HAVE to do this in SQL--if you can do it in ASP or ASP.NET code after getting the data into memory--then it's pretty easy.
 
Old April 7th, 2009, 11:55 PM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

Manoj:

It is not clear as to what you are trying to accomplish. From your example, are you wanting to know which ID has no LChild or RChild, as in a Btrieve Data Structure? If so, then you need to identify the Nodes, as in Root and /or Parent Nodes.
__________________
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.
 
Old April 8th, 2009, 12:34 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm using Sql Server 2005
 
Old April 8th, 2009, 12:36 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I read it as his saying "I want to find the last LEFT node *AND* the last RIGHT node in the chains descending from each parent node."

His example data is far from complete, so it's hard to be sure that's what he meant, but I think that's the idea.

A better example:
Code:
node :: left :: right
  1  ::   4  ::   6
  2  ::   3  ::   8
  3  ::   0  ::   0
  4  ::   5  ::   0
  5  ::   0  ::   0
  6  ::   0  ::   7
  7  ::   0  ::   9
  8  ::   0  ::   0
  9  ::   0  ::   0
So:
for node 1, the left chain is 1=>4=>5 and the right chain is 1=>6=>7=>9
for node 2, the left chain is 2=>3 and the right chain is 2=>8
for node 3, the left chain is just 3 and the right chain is just 3
etc.

So the results returned would be:
Code:
node :: leftmost :: rightmost
  1  ::     5    ::     9
  2  ::     3    ::     8
  3  ::     3    ::     3
etc.
But of course I could be way off.
 
Old April 8th, 2009, 12:39 AM
Authorized User
 
Join Date: Apr 2007
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes sir, that's exactly i want to
 
Old April 8th, 2009, 03:28 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

And does it *HAVE* to be done in SQL??? It would be *MUCH* easier and faster to do it in ASP.NET or ASP code, if that's an option.
 
Old April 8th, 2009, 05:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Or Java, or C++, or C#, or VB.NET... choose your language (it's the SQL forum old pedant... OP could be using anything)...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 8th, 2009, 05:14 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

LOL! Yes, of course. But he said he's using SQL Server, so I [made an *** out of...]d

Hey, I *did* say ASP.NET, so I did cover C# and VB.NET (and J# and ...).
 
Old April 9th, 2009, 02:40 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

Thanks Old Pendant for clearing that up. So in orde to find the Last Left and Last Right Nodes, the Left Child and Right Child would have to be in a certain order: For example, beginning with the 1-ID as the Paremt Node (PN) and "Less than" the parent node is listeddto the left of Parent Node and any child Node greater than the Parent Node is to the Right of the Parent Node.For Brevity sake, after 4 levels of Nodes, a Chhild Node can replace the parent node.
[code]
1=Parent Node .
< > 2
>3
> 6
<5 >7
Hope this did not confuse you. Regardless of which language, the idea is to generate the utility or tool that traverse the structukre.
__________________
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using axis instead of count? Counting child nodes. dep XSLT 1 January 17th, 2007 11:27 AM
count the number of nodes in an XML doc crmpicco Perl 3 December 8th, 2006 08:30 AM
Count nodes based of if condition suri_1811 XSLT 1 December 7th, 2006 08:00 PM
count distinct nodes alexshiell XSLT 2 January 27th, 2005 11:19 AM
Left Join-Count Records On 2nd Table harpua Classic ASP Databases 1 September 16th, 2004 02:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.