Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Grouping Project Data

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 this

ID Task Name ParentID Cost
---- --------------------- --------- -----------
1, Design Activities, null, 0
2, Write Specs, 1, 0
3, Write UI Spec, 2, 45
4, Write Data Spec, 2, 15
5, Build Prototypes, 1, 0
6, Build Screen Mockkups, 5, 20
7, Build Widget Prototype, 5, 15

Notice how actual values exist ONLY at the lowest level

I want to return data like the following:

ID Task Name ParentID Cost
---- --------------------- --------- -----------
1, Design Activities, null, 95
2, Write Specs, 1, 60
3, Write UI Spec, 2, 45
4, Write Data Spec, 2, 15
5, Build Prototypes, 1, 35
6, Build Screen Mockkups, 5, 20
7, 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 CTE

Like this:


/*
ID Task Name ParentID Cost
---- --------------------- --------- -----------
1, Design Activities, null, 0
2, Write Specs, 1, 0
3, Write UI Spec, 2, 45
4, Write Data Spec, 2, 15
5, Build Prototypes, 1, 0
6, Build Screen Mockkups, 5, 20
7, Build Widget Prototype, 5, 15

Notice how actual values exist ONLY at the lowest level

I want to return data like the following:

ID Task Name ParentID Cost
---- --------------------- --------- -----------
1, Design Activities, null, 95
2, Write Specs, 1, 60
3, Write UI Spec, 2, 45
4, Write Data Spec, 2, 15
5, Build Prototypes, 1, 35
6, Build Screen Mockkups, 5, 20
7, 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, 0
UNION SELECT 2, 'Write Specs', 1, 0
UNION SELECT 3, 'Write UI Spec', 2, 45
UNION SELECT 4, 'Write Data Spec', 2, 15
UNION SELECT 5, 'Build Prototypes', 1, 0
UNION SELECT 6, 'Build Screen Mockkups', 5, 20
UNION 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 f1
UNION 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
cte
GROUP BY
[start_Id]
, [task Name]
, [start_parent_Id]
OPTION (MAXRECURSION 0)



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

JohnBGood
Starting Member

48 Posts

Posted - 2009-09-01 : 12:58:21
Thanks!! I will give it a try.

Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -