Solved

T-SQL: SUM problem involving individual rows

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

807 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