That gives me a value of 100 then when I drop the *100 I get a value of 1
Main Topics
Browse All TopicsHello:
I am trying to calculate two columns that are nvarchar(50) using this statement
SELECT AName, CAST(Count(TotalA) AS INT)/CAST(Count(TotalB) AS INT)*100 AS GrandTotal FROM DATA
In the GrandTotal Column 0 is shown for the caculation. However when I use +, -, or * expressions both columns are calculated and a result is shown
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You need to combine the suggestions.
The ISNULL() recommendation was trying to get rid of NULLs from the count which since you are counting over same rows with no conditional will result in same count in the numerator and denominator; hence, 1 is always result. So ADD conditional (CASE) statement.
To make this work, a good approach is to use SUM() versus COUNT().
e.g., SUM( CASE WHEN TotalA IS NOT NULL THEN 1 ELSE 0 END )
SUM( CASE WHEN TotalA IS NOT NULL THEN 1 END ) also works...
The conversion / casting to decimal removes the issue of integer division in SQL. However, you can overcome this by multiplying by 1.0 also. Whenever I am dealing with percentages or anything that I multiply by 100 later I usally take advantage of that and just do that in the numerator.
Here is what I mean all together :
SELECT AName
, SUM( CASE WHEN TotalA IS NOT NULL THEN 1 END ) * 100.0 / SUM( CASE WHEN TotalB IS NOT NULL THEN 1 END ) AS GrandTotal
FROM DATA
GROUP BY AName
;
And taking into consideration division by zero, you can do this :
SELECT AName
, CASE B WHEN 0 THEN 0 ELSE A * 100.0 / B END AS GrandTotal
FROM (
SELECT AName
, SUM( CASE WHEN TotalA IS NOT NULL THEN 1 END ) AS A
, SUM( CASE WHEN TotalB IS NOT NULL THEN 1 END ) AS B
FROM DATA
GROUP BY AName
) derived
;
Hope that helps.
And actually, the aggregates should be filtering NULLs, so you probably have empty strings instead of NULLs.
SELECT AName
, CASE B WHEN 0 THEN 0 ELSE A * 100.0 / B END AS GrandTotal
FROM (
SELECT AName
, SUM( CASE WHEN NULLIF(TotalA, '') IS NOT NULL THEN 1 END ) AS A
, SUM( CASE WHEN NULLIF(TotalB, '') IS NOT NULL THEN 1 END ) AS B
FROM DATA
GROUP BY AName
) derived
;
Above uses NULLIF to force '' to NULL so we can evaluate NULLs and '' the same.
Sorry it must be late, one last one. As others have posted, it should work with very simple COUNT() so this should also work :
SELECT AName
, CASE B WHEN 0 THEN 0 ELSE A * 100.0 / B END AS GrandTotal
FROM (
SELECT AName
, COUNT( NULLIF(TotalA, '') ) AS A
, COUNT( NULLIF(TotalB, '') ) AS B
FROM DATA
GROUP BY AName
) derived
;
I suspect you may have some values in TotalA and/or TotalB that are not able to be cast to numeric (i.e. Apha characters). Try this:
SELECT AName, SUM(CAST(CASE WHEN ISNUMERIC(TotalA) THEN TotalA ELSE 0 END AS DECIMAL(10,2))) /
SUM(CAST(CASE WHEN ISNUMERIC(TotalB) THEN TotalB ELSE 0 END AS DECIMAL(10,2))) * 100 AS GrandTotal
FROM DATA
Are you already showing us the grouped data by AName?
You indicate you want to count them and get a score, but the results appear to be percentage of B to A which is reverse of what showed earlier by the way. Unless you have a type-o in the column headers above.
Select AName, TotalA, TotalB, TotalB * 100.0 / TotalA As Grandtotal
From DATA
;
Should give you the results above starting from data structure you gave. Therefore, the COUNT() you were previously using leads me to believe the raw data is different, but maybe you simply need what I just posted.
mwvista1: I am not sure what you mean. My raw data isnt any different then what I originally posted. My last post was an example in response to MuhammadKashif. Maybe my example was not clear.
I have about 200 rows of yes values in TotalA and no values in TotalB. No null values in either column. I want to count them and divide TotalA by TotalB then multiply it by 100 to get a percentage score; which is the grand total column. Does my example make sense?
Definitely not clear and didn't see any original posting. Just what you last did which showed integer values in the columns and the desired output of :
AName TotalA TotalB GrandTotal
Dan 20 12 60
Makes me think you need 12 / 20 which is 3 / 5 OR 60%.
"I have about 200 rows of yes values in TotalA and no values in TotalB. No null values in either column. I want to count them and divide TotalA by TotalB then multiply it by 100 to get a percentage score; which is the grand total column."
If you have 200 rows of each value with NO null values; then, when you count each column, you will and should get 200. Subsequently, 200 / 200 OR x / x will always be 1.
So again, there must be something else you need to do the calculation appropriately, so it would be best if you actually showed a sample of the raw data and we can construct the query that may help.
Regards,
Kevin
Hello:
OK. I figured out part of the problem, the data was imported into the database wrong. It makes sense that every calculation would be 1. Anyway, I am still having problems calculating the columns. With the new data Column TotalA will have a combination of yes no and maybe responses and column TotalB will have all yes responses. I still need to divide column TotalB by TotalA where TotalA = 'Yes' to get a total.
Sorry for the confusion I was concentrating more on than the sql statement than the actual data in the database.
That makes a little more sense.
Select AName, A, B
, A * 100.0 / B As GrandTotal
From (
Select AName
, Count(Case TotalA When 'Yes' Then 1 End) As A
, Count(TotalB) As B
From DATA
Group By AName
) derived
;
Since TotalA is the column that has conditional and your previous post on expected results, I showed with TotalB (total rows) as the denominator; however, if you truly want TotalB / TotalA to get grand total then just switch those values around. Since A is based on a conditional, then I would add in the "Case A When 0 Then 0 Else ... End" syntax shown previously to prevent divide by 0 errors which can occur when no records in TotalA column equal 'Yes'.
Hope that helps.
Business Accounts
Answer for Membership
by: leakim971Posted on 2009-10-09 at 18:48:38ID: 25540303
Hello RecipeDan,
Try :
Select allOpen in new window