Home > Software engineering >  How can I get proportion of total value in SQLite
How can I get proportion of total value in SQLite

Time:06-29

Nationality Value Prop
n1 1000
n1 2000
n2 2000
n3 3000
n3 3000

RESULT

Nationality Value Prop
n1 3000 27,27%
n2 2000 18,18%
n3 6000 54,54%

CodePudding user response:

Try with this one:

WITH cte AS (
    SELECT nationality,
           SUM(value_eur) AS Value_
    FROM Fifadf
    GROUP BY nationality
)
SELECT nationality,
       Value_,
       Value_ / SUM(Value_) OVER() AS Prop
FROM cte

Check the demo here.

CodePudding user response:

Nationality Value Prop
n1 1000
n1 2000
n2 2000
n3 3000
n3 3000

RESULT

Nationality Value Prop
n1 3000 27,27%
n2 2000 18,18%
n3 6000 54,54%

CodePudding user response:

Try the following:

select distinct nationality,
sum(value_eur) over (partition by nationality) as Value ,
1.0 * sum(value_eur) over (partition by nationality)/(select sum(value_eur) from MyData) as Prob
from MyData

See a demo from db-fiddle.

  • Related