I have data like this:
Task | Hours
1.1 | 40
2 | 40
2.1 | 60
2.1.1 | 15
15.9 | 24
16 | 5
19.1 | 40
19.1.1 | 8
19.1.2 | 12
19.2 | 6
19.2.1 | 21
19.2.2 | 15
19.2.3 | 2
19.3 | 64
I would like to group based on the first two levels of the Task, producing this result:
Task | Hours
1.1 | 40
2 | 40
2.1 | 75
15.9 | 24
16 | 5
19.1 | 60
19.2 | 44
19.3 | 64
I want the 16 to not roll up what’s beneath it, but I need all the other levels to roll up. This is SQL Server 2005. I would normally do a split on the decimal, and break it out that way, but I was wondering if there’s a better way to do it in SQL.
Is changing the model an option? If your task column is really meant to represent a hierarchy, you should really be representing the hierarchy properly in your relational model.
If the number of levels deep is fixed at three, another option might be to add three columns to represent each of the “parts” of the task column independently.
If that’s not an option, I think you can achieve this with a series of CASE statements that parse the string (plus SUM and GROUP BY).
UPDATE:
Ok, this seemed like a fun challenge, so I came up with this: