This question is relevant to SQL Server. I have table with column called gender and 1 or 0 will be saved as integer. I want to know whether there is a method assign a label when I selecting it using SELECT statement.
For example
SELECT gender
FROM emp
AND if 1 occur Male AND 0 occur Female should be displayed.
Use the CASE operator, i.e. CASE gender when 1 then 'Male' else 'Female' end.
Full select as follows:
SELECT gender,
CASE gender
when 1 then 'Male'
else 'Female'
end as gender_name
FROM emp
UPD:
Option with N/A to process all possible optios (if gender field value not in range [0,1], but I prefer to use check constraint to determine possible values, to avoid weird/unexpected values):
SELECT gender,
CASE
gender
when 1 then 'Male'
when 0 then 'Female'
else 'N/A'
end as gender_name
FROM emp
You can use CASE:
SELECT CASE WHEN gender = 1 THEN 'Male' ELSE 'Female' END AS gender
FROM emp
You can use a CASE...WHEN...THEN...END for this
select case when GENDER = 1 then 'Male'
when GENDER = 0 then 'Female'
else 'N/A'
end as GENDER
from emp;
Find more about this here
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 have a table named user. In this table, there is a column named 'Sex', which has value 'm' or 'f'. I want to present these values as 'male' or 'female' without changing the exact value in the database. What is the best way?
Use a CASE expression:
SELECT
Sex,
CASE Sex WHEN 'm' THEN 'male'
WHEN 'f' THEN 'female' END AS SexLabel
FROM yourTable;
Using a CASE expression since you have only 'm' and 'f'
SELECT
Sex,
CASE Sex WHEN 'm' THEN 'male'
else 'female' END AS SexLabel
FROM yourTable;
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;