Link to home
Start Free TrialLog in
Avatar of rhservan
rhservanFlag for United States of America

asked on

How can I sum a column as an expression?

In my VarChar Column,  'ZZ',  I have 3  possibilities with a total of 134 rows :

Possibilities
1.  aaaa
2.  bbbb
3.  cccc

I need to create a table to show a total of each of these possibilities that are listed.

Example:
aaaa    bbbb    cccc
 53         47        34        TOTAL
`
Please provide an expression that I can use to obtain each of  the 3  totals.
Avatar of sameer2010
sameer2010
Flag of India image

Try this
select 
sum(case when possibility = 'aaa' then 1 else 0 end) possaaa,
sum(case when possibility = 'bbb' then 1 else 0 end) possbbb,
sum(case when possibility = 'ccc' then 1 else 0 end) possccc,
sum(case when possibility = 'aaa' then 1 else 0 end) + sum(case when possibility = 'bbb' then 1 else 0 end) + sum(case when possibility = 'ccc' then 1 else 0 end) posstotal
from your_table

Open in new window

Avatar of rhservan

ASKER

Sameer,

There seems to be some missing information on line five at the end.
SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial