0

How can I get the count for the sex(Male, Female) and total in the columns?

enter image description here

i want this in below format how is that possible in Sql.

enter image description here

peterh
  • 11,875
  • 18
  • 85
  • 108
  • Please post sample data, and format your question to show that data along with your expected output as _text_, not images. – Tim Biegeleisen Nov 19 '17 at 07:47
  • If you tag with a RDBMS then I can also give you a more specific duplicate, i.e. SQL Server, MySQL, Oracle etc. But, you also know the keyword `pivot` so can just search for that along with the RDBMS and you'll find the RDBMS specific version in addition to the generic one. – Ben Nov 19 '17 at 08:30

2 Answers2

2

You can use a eg: sum of case when

 select sum(case when sex = 'Female' then 1 else 0 end) Female,
          sum(case when sex = 'Male' then 1 else 0 end) Male,
          sum(case when sex = 'Female' then 1 else 1 end) Total
 from my_table
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

Since you did not provide any other details like the table name, my best effort for you will be something like the following code, This is written in MySql syntax, but I'm sure that in other SQL it will be similar.

SELECT 
    IF(Sex = 'Female', Count, 0) AS Female,
    IF(Sex = 'Male', Count, 0) AS Male,
    SUM(Count) AS Total
FROM
    [TableName]
;
backbone
  • 98
  • 1
  • 1
  • 8