Solved

T-SQL: SUM problem involving individual rows

Posted on 2006-11-27
2
378 Views
Last Modified: 2008-03-10
Hi everyone,
I have a table:

Id               Value
1                10
2                20
3                40
4                10

And I want to select the Ids, Values, and the proportion of the total of the values that each individual row comprises.  Perhaps it's best illustrated: I want to do a SELECT that results in this:

Id               Value           Proportion
1                10               0.125  (10 / 80 (which is the sum of the Values column))
2                20               0.25 (20 / 80)
3                40               0.5 (40 / 80)
4                10               0.125 (10 / 80)

What's the simplest SQL that can give me these results?

Thanks!
-Mike
0
Comment
Question by:bignis
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 69 total points
ID: 18022983
select id, value , value / ( select sum(value) from yourtable)
from yourtable

or:

declare @sum numeric(20,3)
select @sum = sum(value) from yourtable
select id, value , value / @sum from yourtable
0
 

Author Comment

by:bignis
ID: 18027514
Yup, works for me.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now