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 2000 Forums
 SQL Server Development (2000)
 Calculatin percent

Author  Topic 

sardinka
Posting Yak Master

142 Posts

Posted - 2006-09-27 : 08:54:03
how do i calculate something like this if I have the table with names and count?
Name Count Percent
Name1 27 4.69%
Name2 2 0.35%
....
Totals 576 100.00%

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-27 : 08:57:03
Why do you keep posting same question again?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=72598


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-09-27 : 09:08:18
Have you ever tried the solution given by nr?

I think that should work (of course, if that is what you meant).

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-27 : 09:11:17
[code]-- prepare test data
declare @test table (Name varchar(10), count int)

insert @test
select 'Name1', 27 union all
select 'Name2', 2

-- do the work
select name,
sum(count) 'Count',
sum(count) / w.i 'Percent'
from @test
cross join (
select 1.0 * sum(count) i
from @test
) w
group by name,
w.i
union
select 'Total',
(SELECT SUM(count) FROM @test),
100.0[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-09-27 : 09:14:33
Or simpler, if no duplicates in the original table
select		t.name,
t.count 'Count',
t.count / w.i 'Percent'
from @test t
cross join (
select 1.0 * sum(count) i
from @test
) w
union
select 'Total',
(SELECT SUM(count) FROM @test),
100.0


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -