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.
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
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;
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
If I want to show the total number of male patients and the total number of female patients in the patients table having gender as a column as:
male_count female_count
1220 1105
How do I do this ?
structure of patients table (patient_id INT, first_name CHAR, last_name CHAR, city CHAR, birth_date DATE and gender CHAR)
Entries in gender column include 'M' and 'F'
I am trying to learn SQL. Please revert with a possible solution
You can use subqueries like this
SELECT
(SELECT count(*) FROM patients WHERE gender = 'F') as female_count,
(SELECT count(*) FROM patients WHERE gender = 'M') as male_count
;
Not the prettiest way to do it, but should work
You can use CASE statements and SUM the results for each column.
SELECT
SUM(CASE WHEN gender = 'M' THEN 1 ELSE 0 END) AS male_count,
SUM(CASE WHEN gender = 'F' THEN 1 ELSE 0 END) AS female_count
FROM patients;
Currently I have data in sql and need to merge the counts from rows.
My code is:
WHERE lower(GENDER) IS NOT NULL
GROUP BY lower(GENDER)
And it's out putting a table like:
Gender Count
female 100
f 101
male 102
m 103
unknown 104
Is there a way to combine the counts from female and f and then similarly from male and m?
Use a case expression:
select
case lower(gender)
when 'f' then 'female'
when 'm' then 'male'
else lower(gender)
end new_gender,
count(*) cnt
from mytable
where gender is not null
group by case lower(gender)
when 'f' then 'female'
when 'm' then 'male'
else lower(gender)
end
Note that you don't need lower() to check if gender is null.
Some databases support positional parameters in the group by clause, so you can just do:
group by 1
Other databases support re-using aliases defined in the select clause:
group by new_gender
Sure you just need to somehow list how that's going to happen. This can be done using a function or a case statement.
A case statement is a fine solution for a one off query, but I'd recommend a conversion table or a function if you're going to be spreading this across multiple queries.
select
case
when lower(gender) = 'f' or lower(gender) = 'female'
then 'female'
when lower(gender) = 'm' or lower(gender) = 'male'
then 'male'
end as cleanded_gender
, count(*) as gender_count
from
...
where
gender is not null
group by
case
when lower(gender) = 'f' or lower(gender) = 'female'
then 'female'
when lower(gender) = 'm' or lower(gender) = 'male'
then 'male'
end
Depending on your RDBM you can take the substring of Gender, only the first char,
and it will group on "f", "m", "u"... not nice, but it will work
I wanted to a count of the same field for different values for example:
user{user_id, gender}
Gender can have obviously male or female :)
i want to get count for all the males and females i.e.
COUNT(male) COUNT(female)
4 16
but i'm confused because they come from the same gender column thanks
ALSO, I Want the result to only display the higher count. Like
COUNT(female)
16
Try the following using case statement.
select
sum(case when gender = 'male' then 1 else 0 end) as total_male,
sum(case when gender = 'female' then 1 else 0 end) as total_female
from user
If you are using MySQL then use following
select
sum(gender = 'male') as total_male,
sum(gender = 'female') as total_female
from user
If you are using PostgreSQL then use filter
select
count(1) filter (where gender = 'male') as total_male,
count(1) filter (where gender = 'female') as total_female
from user
You can achieve your final result by following query. here is the demo.
select
case
when total_male < total_female then total_female
else total_male
end as total_count
from
(
select
count(1) filter (where gender = 'male') as total_male,
count(1) filter (where gender = 'female') as total_female
from users
) t
I want to work out the male/female split of my customer based on the person's title (Mr, Mrs, etc)
To do this I need to combine the result for the Miss/Mrs/Ms into a 'female' field.
The query below gets the totals per title but I think I need a sub query to return the combined female figure.
Any help would be greatly appreciated.
Query:
SELECT c.Title, COUNT(*) as Count
FROM
Customers c
GROUP BY Title
ORDER By [Count] DESC
Answer:
Mr 1903
Miss 864
Mrs 488
Ms 108
You could do it like this
SELECT
[Gender] = CASE [Title] WHEN 'Mr' THEN 'M' ELSE 'F' END,
COUNT(*) as Count
FROM
Customers c
GROUP BY
CASE [Title] WHEN 'Mr' THEN 'M' ELSE 'F' END
ORDER By
[Count] DESC
Demo at http://sqlfiddle.com/#!3/05c74/4
You can use CASE to project the new groups for the Titles:
SELECT SUM(CASE WHEN Title IN ('Mr') THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN Title IN ('Miss', 'Ms', 'Mrs') THEN 1 ELSE 0 END) AS Female
FROM
Customers c;
Try this:
SELECT (CASE WHEN c.Title = 'Mr' THEN 'Male'
WHEN c.Title IN ('Mrs', 'Miss', 'Ms') THEN 'Female'
ELSE 'NA'
END) AS title,
COUNT(1) AS PeopleCount
FROM Customers c
GROUP BY (CASE WHEN c.Title = 'Mr' THEN 'Male'
WHEN c.Title IN ('Mrs', 'Miss', 'Ms') THEN 'Female'
ELSE 'NA'
END)
ORDER By PeopleCount DESC;
Following query returns a number of people having the same name with gender = Male.
select lookup_name.firstname,count(lookup_name.firstname)
from lookup_name
where gender='M'
group by firstname
similarly, the query below returns a number of people having the same name with gender = Female.
select lookup_name.firstname,count(lookup_name.firstname)
from lookup_name
where gender='F'
group by firstname
I need to write a query which finds out the name and tell the gender (whether male or female) with the greater count. i.e higher probability of that name in the database is of being male or female?
SELECT firstname, Male, Female,
case when Male=Female then 'indeterminate'
when Male>Female then 'probably male'
else 'probably female' end MostProbablySex
FROM (
select firstname,
SUM(case when gender='M' then 1 else 0 end) Male,
SUM(case when gender='F' then 1 else 0 end) Female
from lookup_name
group by firstname
) X;
Or a single pass:
select firstname,
CASE SIGN(2.0 * SUM(case when gender='M' then 1 else 0 end) / COUNT(*) - 1)
WHEN -1 then 'probably female'
WHEN 0 then 'indeterminate'
WHEN 1 then 'probably male'
END
from lookup_name
group by firstname;