Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading