Solved

T-SQL: SUM problem involving individual rows

Posted on 2006-11-27
2
377 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now