realkes
realkes

Reputation: 881

Calculating the ratio of each item to the column in SQL

I have a data like this:

col1 value
A     2
B     4
C     8   
D     6

I want to calculate the ratios to the column total

col1 value
A     10%
B     20%
C     40% 
D     30%

How can I do this with SQL?

Upvotes: 0

Views: 182

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269583

You can use window functions:

select t.*, value * 100.0 / sum(value) over () as perentage
from t;

Upvotes: 1

Related Questions