0

This is how my table looks like,Here is 1 is male and 2 is female

Id  Gender
1     1
2     2
3     2
4     1

this is how i expected the answer

percentage

code that i tried

Select 
 Pt.Id,
 Pt.Gender,
 sum(case when Gender= 1 then 1 else 0 end) as maleCount,
 sum(case when Gender= 2 then 1 else 0 end) as femalecount

 from Patients Pt
 WHERE Pt.Isactive = 1 and Pt.IsDeleted=0  
 Group by Pt.Id,Pt.Gender

I want to get the male and female percentage.How can i achieve it

Mike Micky
  • 21
  • 6
  • You're grouping by the `Id` of the patient here, is that intended? You'll only get one row per patient then, and I suspect that your patients *don't* have multiple genders – Thom A Jan 13 '23 at 10:44
  • As for getting a percentage, a conditional `AVG` is one method. – Thom A Jan 13 '23 at 10:44
  • No it doesn't can you suggest any answer from the above question – Mike Micky Jan 13 '23 at 10:57
  • 1
    Looks to answer the question to me, @MikeMicky . Can you *explain* why it doesn't? – Thom A Jan 13 '23 at 10:59
  • i couldn't understand exactly. Can you give me the exact answer to my question – Mike Micky Jan 13 '23 at 11:20
  • Then explain what you don't understand. Explain why the answers don't answer your question, (when they most certainly do). Demonstrate your attempt(s) to implement the solutions, and what about them aren't working. – Thom A Jan 13 '23 at 11:58

0 Answers0