Search code examples
sqlgroup-bypartitioningpercentage

How to find percentage on in SQL?


I have data like this: Survived  cnt
No        549
Yes       342

I want to find the percentage as a third column for each row, No- 549/(549+342) Yes - 342/(549+342)

I have tried select Survived, cnt, round(100*(cnt/sum(cnt)), 2) as prcntage from survivalCnt and also with grouping but nothing seems to work, I also tried with Partitioning.

Can someone help me, I know its a simple problem, I can't seem to pin point the answer


Solution

  • you need a subquery to run the calculation

    SELECT
        Survived,
        cnt,
        (cnt * 100.0 / (SELECT SUM(cnt) FROM survival_data)) AS "%"
    FROM
        survival_data;