Solved

How can  I sum a column as an expression?

Posted on 2010-11-15
4
931 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:rhservan
  • 2
4 Comments
 
LVL 13

Expert Comment

by:sameer2010
ID: 34141798
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

0
 

Author Comment

by:rhservan
ID: 34142232
Sameer,

There seems to be some missing information on line five at the end.
0
 
LVL 21

Assisted Solution

by:Alfred1
Alfred1 earned 250 total points
ID: 34142304
Try something like this.
SELECT *
FROM (
SELECT (CASE ZZ
           WHEN 'aaaa' THEN 'aaaa'
           WHEN 'bbbb' THEN 'bbbb'
           WHEN 'cccc' THEN 'cccc'
        END) As [Poss],
        (COUNT((CASE ZZ
           WHEN 'aaaa' THEN 'aaaa'
           WHEN 'bbbb' THEN 'bbbb'
           WHEN 'cccc' THEN 'cccc'
        END)) As [SubTotal]
FROM YOURTABLE
GROUP BY ZZ
) TableZZ
PIVOT (
SUM(SubTotal)
FOR [Poss] IN (
[aaaa],[bbbb],[cccc]
)
) PivotTable

Open in new window

0
 
LVL 13

Accepted Solution

by:
sameer2010 earned 250 total points
ID: 34160681
Are you getting any error? What is the error you are getting?
The first 4 lines select the count of non-blank possiblities and 5th row calculates the sum for all non-blank rows. If you do not need total for all possibilities, you can drop that row. So, the query would become
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
from your_table

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS Dynamic Column Names 3 66
Tracking a Database Lock - SQL 2014 10 49
SSRS page break on parameter values 1 48
New to SSRS but trying to fix a poorly performing report 5 16
Written by Valentino Vranken. Introduction: In a previous article (http://www.experts-exchange.com/articles/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Reporting-On-Data-From-Stored-Procedures-part-1.html) I announced that I would writ…
Time Corrections for Reports Working with a report, we made some interesting discoveries about the time corrections/updates We are using the following Parameters: Starting Entered Date (Date) formatted as Data type: "Date/Time" Ending Entered …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question