Merge Rows COUNTS in SQL - sql

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

Related

print out the number of two values in the same column

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');

Is it possible to combine these two sql statements into one statement using group by?

select client_type, count(gender) as num_males
from clients
where gender = 'Male'
group by client_type;
select client_type, count(gender) as num_females
from clients
where gender = 'Female'
group by client_type;
The following SQL statements show the number of males by client type, then the number of females by client type. I would like an SQL statement to show the following columns: client_type, count(gender = 'Male'), count(gender = 'Female'). Is it possible to do this?
You could count a couple of case expressions:
SELECT client_type,
COUNT(CASE gender WHEN 'Male' THEN 1 END) AS num_males,
COUNT(CASE gender WHEN 'Female' THEN 1 END) AS num_females
FROM clients
GROUP BY client_type;

How can i change column value in sql query result, not in database?

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;

Renaming result categories in SQL

I have the following query which outputs the number and percentage of members whose Salutation is Mr / Ms.
I want to rename the results to say 'Male' instead of 'Mr' and Female instead of 'Ms'.
It's probably a fairly simple CASE thing, but can't get it to work...
SELECT AspNetUsers.Salutation AS Sex, COUNT(AspNetUsers.Salutation) as Total,
CAST(ROUND((COUNT(AspNetUsers.Salutation)* 100.0 / (SELECT COUNT(*) FROM Member, AspNetUsers WHERE Member.AspNetUserId = AspNetUsers.Id)),1) AS NUMERIC(36,1)) AS Percentage
FROM Member, AspNetUsers
WHERE Member.AspNetUserId=AspNetUsers.Id
GROUP BY Salutation
You're absolutely correct that it's a simple case expression that is needed, but you also need to group by the same case expression.
SELECT
CASE
WHEN AspNetUsers.Salutation = 'Mr' THEN 'Male'
WHEN AspNetUsers.Salutation = 'Ms' THEN 'Female'
ELSE 'Other' -- this is of course optional
END AS Sex,
COUNT(AspNetUsers.Salutation) as Total,
CAST(ROUND((COUNT(AspNetUsers.Salutation)* 100.0 / (SELECT COUNT(*) FROM Member JOIN AspNetUsers ON Member.AspNetUserId = AspNetUsers.Id)),1) AS NUMERIC(36,1)) AS Percentage
FROM Member
JOIN AspNetUsers ON Member.AspNetUserId = AspNetUsers.Id
GROUP BY
CASE
WHEN AspNetUsers.Salutation = 'Mr' THEN 'Male'
WHEN AspNetUsers.Salutation = 'Ms' THEN 'Female'
ELSE 'Other' -- this is of course optional
END;
The query could probably be improved by using a common table expression to not have to repeat the case expression, and a windowed count instead of a subquery, but I'll leave that to you.

Assign a value when for integer when USE select query

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