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)
 Figuring out Percentages

Author  Topic 

scooternm
Starting Member

8 Posts

Posted - 2008-07-03 : 13:56:21
I have a table that I want to run a query against with the eventual goal to produce some pie charts.

The last column will contain 5 possible values. I want to figure out what percentage each value takes up in the table. So if there are 100,000 entries and

Value1=10,000 times
Value2=40,000 times
Value3=5,000 times
Value4=25,000 times
Value5=20,000 times

I want a query that produces the following results:
10%
40%
5%
25%
20%

Can anyone help point me in the right direction? Thanks.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-07-03 : 14:35:41
DECLARE @table TABLE (Col1 int)

INSERT INTO @table
SELECT 1 union all
SELECT 1 union all
SELECT 1 union all
SELECT 2 union all
SELECT 2 union all
SELECT 5 union all
SELECT 6

SELECT Col1,count(*),count(*)*100.0/(select count(*) from @table)
FROM @table
GROUP BY col1

Jim
Go to Top of Page

scooternm
Starting Member

8 Posts

Posted - 2008-07-03 : 15:18:45
Thanks, I think I got it!
Go to Top of Page
   

- Advertisement -