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
 How to find the sum of rows and then Percentage

Author  Topic 

goodman2253
Yak Posting Veteran

88 Posts

Posted - 2012-03-06 : 04:27:28
Hi All,
I am stuck with an problem and want your help
What i want to do is-I have a query which gives the result as

NAME--Value
A------1
B------2
C------3
D------4
E------5

What i want to do is I have to count the total number of Value Columns
(1+2+3+4+5)=15

and expected output is

NAME--Value--%age
A------1-----6.66%
B------2-----13.33%
C------3-----20.00%
D------4-----26.66%
E------5-----33.33%

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-06 : 04:32:17
[code]
[%age] = Value * 100.0 / sum(Value) over ()
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

goodman2253
Yak Posting Veteran

88 Posts

Posted - 2012-03-06 : 06:01:04
Thanks..:)
Go to Top of Page

goodman2253
Yak Posting Veteran

88 Posts

Posted - 2012-03-06 : 06:31:33
Hi one more thing this function should give as output
6.66%
13.33%
20.00%
26.66%
33.33%
Go to Top of Page

goodman2253
Yak Posting Veteran

88 Posts

Posted - 2012-03-06 : 06:33:08
After a decimal only two character allowed with % sign
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-06 : 06:47:18

you can use ROUND ( <number> , 2 ) to round it to 2 decimal places

or

convert(decimal(10,2) <number> ) to convert it

However, % is a display / formatting issue. Formatting should be performed at your front end application


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

goodman2253
Yak Posting Veteran

88 Posts

Posted - 2012-03-06 : 06:59:55
Round function give as output
6.6600000
13.330000

But I want the output as
6.66
13.33
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-06 : 07:07:17
i have provided 2 method there. Have you try the other one ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -