Link to home
Create AccountLog in
Avatar of Tom Sage
Tom SageFlag for United States of America

asked on

T-SQL - How to do subtotals?

I have a table with customers and a customertype.  I would like to get  a subtotal of the number of customer types.  Here is an example.  Note that the "Total" field is not in the database.

Type  Total
Good   100
Bad      25
Other    9

Thank you
Here is what I have done so far... IT does give a total, but not a subtotal by Type.
 
SELECT    CustType
FROM        Customer
ORDER BY CustType
COMPUTE COUNT(CustType)

Open in new window

Avatar of chapmandew
chapmandew
Flag of United States of America image

select custtype, count(*) as total
from(
SELECT    CustType
FROM        Customer
ORDER BY CustType
COMPUTE COUNT(CustType)
) a
group by custtype
Avatar of Tom Sage

ASKER

Hello,

I am getting this error:

Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'COMPUTE'.

Any ideas?
Thank you
ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73
Flag of Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hello Reb73,

Good job !   That is it exactly !

Thanks for the quick response.

Regards