Sub-Totals

greenspun.com : LUSENET : SQL Server Database Administration : One Thread

I have a table with columns A, B, C and Valu

I want the the result to be displayed like this.

1. Sum of all Valu for distinct A and B combination 2. Sum of all Valu for distinct A 3. Grand sum of all Valu.

And I want the output arranged like this.

a1,b11,sum a1,b12,sum a1,b13,sum a1,b_1all,sum a2,b21,sum a2,b22,sum a2,b_2all,sum a_ll,b_all,sum

Can I do this using a single select statment or with subqueries ? What is the best way (less temporary overhead and less time) of accomplishing this?

-- Anonymous, April 13, 2000

Answers

Mahesh,

The C column seems irrelevant to your question. Assuming that is the case, this is what you are looking for:

SELECT A, B, SUM (Valu)

FROM TableName

GROUP BY A, B WITH ROLLUP

The result set uses NULL for the 'all' rows.

Hope this helps,

Eric

-- Anonymous, April 13, 2000


Moderation questions? read the FAQ