Solved

T-SQL: SUM problem involving individual rows

Posted on 2006-11-27
2
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…

632 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