| Author |
Topic |
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-09-01 : 11:54:29
|
| I have some Hierarchical project data that I would like to do some grouping on.My data looks something like thisID Task Name ParentID Cost---- --------------------- --------- ----------- 1, Design Activities, null, 02, Write Specs, 1, 03, Write UI Spec, 2, 454, Write Data Spec, 2, 155, Build Prototypes, 1, 06, Build Screen Mockkups, 5, 207, Build Widget Prototype, 5, 15Notice how actual values exist ONLY at the lowest levelI want to return data like the following: ID Task Name ParentID Cost---- --------------------- --------- ----------- 1, Design Activities, null, 952, Write Specs, 1, 603, Write UI Spec, 2, 454, Write Data Spec, 2, 155, Build Prototypes, 1, 356, Build Screen Mockkups, 5, 207, Build Widget Prototype, 5, 15 notice how data has been summed for the parent tasks.Is this hard to do? |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-09-01 : 12:23:43
|
Use a recursive CTELike this:/*ID Task Name ParentID Cost---- --------------------- --------- ----------- 1, Design Activities, null, 02, Write Specs, 1, 03, Write UI Spec, 2, 454, Write Data Spec, 2, 155, Build Prototypes, 1, 06, Build Screen Mockkups, 5, 207, Build Widget Prototype, 5, 15Notice how actual values exist ONLY at the lowest levelI want to return data like the following: ID Task Name ParentID Cost---- --------------------- --------- ----------- 1, Design Activities, null, 952, Write Specs, 1, 603, Write UI Spec, 2, 454, Write Data Spec, 2, 155, Build Prototypes, 1, 356, Build Screen Mockkups, 5, 207, Build Widget Prototype, 5, 15 */DECLARE @foo TABLE ( [ID] INT , [Task Name] NVARCHAR(255) , [ParentId] INT , [cost] MONEY )INSERT @foo SELECT 1, 'Design Activities', null, 0UNION SELECT 2, 'Write Specs', 1, 0UNION SELECT 3, 'Write UI Spec', 2, 45UNION SELECT 4, 'Write Data Spec', 2, 15UNION SELECT 5, 'Build Prototypes', 1, 0UNION SELECT 6, 'Build Screen Mockkups', 5, 20UNION SELECT 7, 'Build Widget Prototype', 5, 15; WITH cte AS (SELECT [ID] AS [Start_Id] , [parentId] AS [Start_Parent_Id] , [ID] , [Task Name] , [ParentId] , [cost]FROM @foo f1UNION ALL SELECT f1.[start_Id] , f1.[Start_Parent_Id] , f2.[ID] , f1.[task Name] , f2.[parentId] , f2.[cost]FROM @foo f2 JOIN cte f1 ON f1.[Id] = f2.[parentId] )SELECT [start_Id] , [task Name] , [start_parent_Id] , SUM([cost])FROM cteGROUP BY [start_Id] , [task Name] , [start_parent_Id]OPTION (MAXRECURSION 0) Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-09-01 : 12:27:46
|
| Also -- you need to be sure that your data doesn't contain circular reference. Otherwise the CTE won't produce results it will just die.Shouldn't be a problem based on your sample data.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-09-01 : 12:58:21
|
| Thanks!! I will give it a try. |
 |
|
|
JohnBGood
Starting Member
48 Posts |
Posted - 2009-09-01 : 21:10:15
|
| wow!! works great. Thanks a million!I've got a similar question using time-phased data, but will ask via a separate post. |
 |
|
|
|
|
|