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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 count the specific value in the whole table

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2008-05-08 : 16:49:21
I am trying to count the number of value "99" for each column in the whole table
to see how many "99" are there per column for the whole table...
and get the percentage per each column.. How can I do this?

output should look like

Column A 117 10%
Column B 120 14%
-- etc..




SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-08 : 17:17:39
select col1 / t as col1,
col2 / t as col2,
col3 / t as col3
from (
select sum(case when col1 = 99 then 1.0 else 0.0 end) as col1,
sum(case when col2 = 99 then 1.0 else 0.0 end) as col2,
sum(case when col3 = 99 then 1.0 else 0.0 end) as col3,
count(*) AS t
from table1
) as d



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -