Muflix
Muflix

Reputation: 6778

How to calculate ratio of two values in the table for specific key

I have following table

enter image description here

How can I get ratio values ?

The ratio between the values in the "Value" column, the sum is always 1 as 100%.

enter image description here

the number of rows in the table is not limited, there can be more Number1, Number2, Code combinations.

SQL Fiddle http://sqlfiddle.com/#!3/95fc1

Upvotes: 0

Views: 6032

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

Try this:

SELECT Number1, Number2, 
       [Ratio]  / (SUM([Ratio]) OVER (PARTITION BY Number1, Number2) * 1.0)
FROM Example

Demo here

Upvotes: 1

Related Questions