Home > Software design >  print out the number of two values in the same column
print out the number of two values in the same column

Time:01-18

I have a database in which there are people. They have genders. How would I count male and female separate.

SELECT count(id_osb)
from ds_osebe
where spol = 'M'
or spol = 'Z';

this is how i can get the number of male and female combined

I do not know how to make this, it's my second day learning this.

CodePudding user response:

You need to use grouping (group by)

SELECT spol, count(id_osb)
from ds_osebe
where spol = 'M'
or spol = 'Z'
group by spol

NOTE: replace spol if it doesn't denote sex

CodePudding user response:

You can use a CASE expression.

Query

select SUM(case spol when 'M' then 1 else 0 end) as male_cnt
SUM(case spol when 'Z' then 1 else 0 end) as female_cnt
from ds_osebe;

CodePudding user response:

select count(1) over (partition by spol) as qty, spol
from ds_osebe
/*if you have more than 2 gender options*/
where spol in ('Z', 'M');
  • Related