Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 COUNT function with GROUP BY

Author  Topic 

HeyZain
Starting Member

7 Posts

Posted - 2010-02-01 : 15:16:29
I have a table with 3 columns:

Unit, Month, Percentage

I need a column something like this:

SELECT Unit, COUNT(Percentage WHERE Percentage >= 80.00)/COUNT(Percentage) * 100 AS [MyColumn]
WHERE Month = @Month
GROUP BY Unit

I am stuck at implementing a good working solution for this.

Please note that, the second COUNT function will count all percentages (whether >= 80.00 or <= 80.00)

Please help.

Thanks,
Zain

Z@in

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-02-01 : 15:36:38
This should work:
select
Unit,
sum(case when Percentage >=80.00 then 1 else 0 end) / COUNT(Percentage) * 100 AS [MyColumn]
WHERE [Month] = @Month
GROUP BY Unit


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -