We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Count Data then Sum Rows

RecipeDan
RecipeDan asked
on
Medium Priority
1,608 Views
Last Modified: 2012-06-27
Hello:

I have this SQL Statement: SELECT Count(FinalData) AS Total FROM Data
It displays the results in a row like this:

34
78
44
90
35

The results are correct. How can I sum the results after I count them?
 
Comment
Watch Question

Commented:
Add the following at the end of the SELECT statement -

COMPUTE SUM(COUNT(FINALDATA))
Nathan RileyFounder

Commented:

SELECT sum(Count(FinalData)) AS Total FROM Data

Open in new window

Commented:
Gallitin -> Your query will yield the following error message -

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

The count(finaldata) in the original query is resulting in multiple rows because there is a grouped field which has not been indicated in the question. Otherwise a mere count(finaldata) should suffice..
Nathan RileyFounder

Commented:
I just tested in query analyzer a minute ago against a tabel of mine, I must have done something different then
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
SELECT SUM(Total) AS GrandTotal
FROM (
SELECT Count(FinalData) AS Total FROM Data
) AS derived

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
SELECT COUNT(FINALDATA)  AS Total
FROM Data
COMPUTE SUM(COUNT(FINALDATA))

This gives me the same result
34
78
44
90
35

SharathData Engineer
CERTIFIED EXPERT

Commented:
What is your expected result? Are you looking for this? Do yu have GROUP BY clause in your original query?

SELECT SUM(Total) FROM (SELECT COUNT(FINALDATA) AS Total FROM Data ) T1
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.