Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Find sub-tree nodes Expand / Collapse
Author
Message
Posted Monday, October 08, 2007 10:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 23, 2009 4:07 PM
Points: 21, Visits: 11
Hi,



I have a tree structure represented by a simple table that references itself:


CREATE TABLE [dbo].[AframeGroup] (

[GroupID] [int] IDENTITY (1, 1) NOT NULL ,

[ParentGroupID] [int] NOT NULL ,

[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

) ON [PRIMARY]




I need write an SP (or function?) that accepts a parameter of a GroupID and returns a Table of all GroupIDs that are below the GroupID supplied as a parameter.



This is the current Group structure (see below).



If I suppliy 12 as a parameter, I need to get all sub-IDs returned in a table (ie 13, 14, 15, 16, 17, 18, 19).



If I suppliy 6 as a parameter, I need to get all sub-IDs returned in a table (ie 7, 8, 9, 10, 11, 12 13, 14, 15, 16, 17, 18, 19, 20, 21).





----All (ID = 6)

--------Direct Sales (ID = 7)

--------Inbound Calls (ID = 8)

------------QLD (ID = 10)

------------NSW (ID = 11)

------------Melbourne (ID = 12)

----------------Project A (ID = 13)

--------------------General (ID = 14)

--------------------Japanese (ID = 15)

--------------------Trade (ID = 16)

--------------------Outbound (ID = 17)

--------------------Holidays (ID = 18)

----------------Project B (ID = 19)

------------St Kilda Road (ID = 20)

------------Geelong (ID = 21)



FYI: The SP that generated the text above is this SP:




ALTER PROCEDURE apBL_Group_GetSubTreeItems (@RootID int)

AS

BEGIN

DECLARE @GroupID int, @GroupName varchar(30)

SET @GroupName = (SELECT [Name] FROM AframeGroup WHERE GroupID = @RootID)

PRINT REPLICATE('-', @@NESTLEVEL * 4) + @GroupName + ' (ID = ' + CAST(@RootID AS VarChar(20)) + ')'

SET @GroupID = (SELECT MIN(GroupID) FROM AframeGroup WHERE ParentGroupID = @RootID)

WHILE @GroupID IS NOT NULL

BEGIN

EXEC dbo.apBL_Group_GetSubTreeItems @GroupID

SET @GroupID = (SELECT MIN(GroupID) FROM AframeGroup WHERE ParentGroupID = @RootID AND GroupID > @GroupID)

END

END



The problem is how do I store the GroupID during the recursion so that I end up with a table of all the GroupIDs?

Post #408290
Posted Monday, October 08, 2007 11:45 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 24, 2013 5:02 AM
Points: 2,365, Visits: 1,825
Hi

This is what CTE's are there for in sql2005 - recursive querying. check out BOL for details on how to use CTE in case you dont know.

How ever CTE's do have some performance issues if the number records to be returned is large.



"Keep Trying"
Post #408295
Posted Tuesday, October 09, 2007 3:02 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 9:53 AM
Points: 1,501, Visits: 18,208
Try this

DECLARE @RootID INT

SET @RootID=12;

WITH CTE AS(
SELECT GroupID,ParentGroupID
FROM AframeGroup
WHERE ParentGroupID=@RootID
UNION ALL
SELECT a.GroupID,a.ParentGroupID
FROM AframeGroup a
INNER JOIN CTE c ON c.GroupID=a.ParentGroupID)
SELECT GroupID
FROM CTE
ORDER BY GroupID


____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #408331
Posted Thursday, October 04, 2012 9:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, May 08, 2013 1:38 PM
Points: 132, Visits: 336
Mark-101232 (10/9/2007)
Try this

DECLARE @RootID INT

SET @RootID=12;

WITH CTE AS(
SELECT GroupID,ParentGroupID
FROM AframeGroup
WHERE ParentGroupID=@RootID
UNION ALL
SELECT a.GroupID,a.ParentGroupID
FROM AframeGroup a
INNER JOIN CTE c ON c.GroupID=a.ParentGroupID)
SELECT GroupID
FROM CTE
ORDER BY GroupID


Eventually I run into performance issue when number records being so large. Could you suggest another method of doing tree drill down?

Thanks!
Post #1368501
Posted Thursday, October 04, 2012 10:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:39 AM
Points: 2,556, Visits: 4,398
I've not checked when OP did post original question...



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1368542
Posted Thursday, October 04, 2012 10:18 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641, Visits: 8,273
haiao2000 (10/4/2012)


Eventually I run into performance issue when number records being so large. Could you suggest another method of doing tree drill down?

Thanks!


You do realize this thread is 5 years old? You really should just start your own thread.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1368553
Posted Thursday, October 04, 2012 6:11 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:24 PM
Points: 2,346, Visits: 3,192
Just goes to show that threads may die off in our memories but they live on forever in the heart of Google!


No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1368786
Posted Friday, October 05, 2012 9:30 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Monday, May 20, 2013 1:53 AM
Points: 1,474, Visits: 2,342
Old threads don't die, they just get flushed to disk...
Post #1369148
Posted Friday, October 05, 2012 9:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, May 08, 2013 1:38 PM
Points: 132, Visits: 336
Sean Lange (10/4/2012)
haiao2000 (10/4/2012)


Eventually I run into performance issue when number records being so large. Could you suggest another method of doing tree drill down?

Thanks!


You do realize this thread is 5 years old? You really should just start your own thread.


I didn't post an answer didn't I? I could have started new thread, but thought it was just simple question on the same topic. plus it will get populated to top of the list anyway...so why bother
Post #1369151
Posted Monday, October 08, 2012 11:31 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 32,923, Visits: 26,811
haiao2000 (10/4/2012)
Mark-101232 (10/9/2007)
Try this

DECLARE @RootID INT

SET @RootID=12;

WITH CTE AS(
SELECT GroupID,ParentGroupID
FROM AframeGroup
WHERE ParentGroupID=@RootID
UNION ALL
SELECT a.GroupID,a.ParentGroupID
FROM AframeGroup a
INNER JOIN CTE c ON c.GroupID=a.ParentGroupID)
SELECT GroupID
FROM CTE
ORDER BY GroupID


Eventually I run into performance issue when number records being so large. Could you suggest another method of doing tree drill down?

Thanks!


Nested Sets. HierarchyID. Materialized path with a level based splitter.

How many nodes do you have in your Hierarchy and how often is it updated?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1369969
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse