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.
| 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 |
|
|
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 AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-27 : 09:11:17
|
| [code]-- prepare test datadeclare @test table (Name varchar(10), count int)insert @testselect 'Name1', 27 union allselect 'Name2', 2-- do the workselect name, sum(count) 'Count', sum(count) / w.i 'Percent'from @testcross join ( select 1.0 * sum(count) i from @test ) wgroup by name, w.iunion select 'Total', (SELECT SUM(count) FROM @test), 100.0[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-27 : 09:14:33
|
Or simpler, if no duplicates in the original tableselect t.name, t.count 'Count', t.count / w.i 'Percent'from @test tcross join ( select 1.0 * sum(count) i from @test ) wunion select 'Total', (SELECT SUM(count) FROM @test), 100.0 Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|