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 2008 Forums
 Transact-SQL (2008)
 Hierarchical Query with Subtotal

Author  Topic 

raymon90
Starting Member

7 Posts

Posted - 2011-12-07 : 06:03:53
Hi all, first time posting here. I'm trying to create a table using CTE.
Is it possible to add sub total for each parent and add grand total as the last row. Below is the illustration of the table i would like to create.

Parent 1
Child 1.1
Grandchild 1.1.1
Grandchild 1.1.2
Child 1.2
Grandchild 1.2.1
Sub Total
Parent 2
Child 2.1
Grandchild 2.1.1
Grandchild 2.1.2
Child 2.2
Grandchild 2.2.1
Sub Total
Grand Total

Thanks,
Raymon.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-07 : 06:41:51
its very easy if you're doing this in a report. which is your front end tool by the way?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raymon90
Starting Member

7 Posts

Posted - 2011-12-07 : 06:50:07
Umm.. not really sure what do you mean by front end tool. I'm using SQL Server 2008 R2. How do you make such query?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-07 : 06:58:42
i'm asking where you want to show the data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raymon90
Starting Member

7 Posts

Posted - 2011-12-07 : 07:04:28
Ohh.. in a word document. I'm generating a report using coldfusion + a document generator tool called aspose.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-07 : 07:45:52
quote:
Originally posted by raymon90

Ohh.. in a word document. I'm generating a report using coldfusion + a document generator tool called aspose.


coldfusion should have a way to add the subtotals easily. most of reporting tools have it

if you want to get this in sql you need to use WITH CUBE or WITH ROLLUP statements

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raymon90
Starting Member

7 Posts

Posted - 2011-12-07 : 21:47:33
Hmm.. Do you have any sample code? Actually I just want to make it works first in SQL Server. Then I need to make some modification.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-08 : 00:45:03
quote:
Originally posted by raymon90

Hmm.. Do you have any sample code? Actually I just want to make it works first in SQL Server. Then I need to make some modification.


see the below

http://databases.about.com/od/sql/l/aacuberollup.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raymon90
Starting Member

7 Posts

Posted - 2011-12-09 : 01:43:45
Thanks for the link. But can is it possible to use Cube or Rollup with Common Table Expression? I'm doing a parent-child query for the table. Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-09 : 01:52:23
its not possible inside CTE. why not use it in select statement from cte?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raymon90
Starting Member

7 Posts

Posted - 2011-12-09 : 05:20:38
Still have no idea on how to make the query. This is the sample table which I would like to create: http://imageshack.us/f/856/page17q.jpg/
Have any idea on how to query the table?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-09 : 05:24:25
sorry cant view link here. will take a look later and reply

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raymon90
Starting Member

7 Posts

Posted - 2011-12-14 : 00:18:30
Hi Visakh, have you checked out the image link I posted? Got any idea on how to create the query? Thanks.
Go to Top of Page
   

- Advertisement -