Match "MALE" but not "FEMALE" in SQL - sql

I have a string column with the following values
Genders
MALE; FEMALE; NON_BINARY
MALE
MALE; FEMALE
FEMALE
I want to create two indicators: one if the Genders field contains the value MALE and another if it contains the value FEMALE.
Originally, I was just doing
,CASE WHEN genders LIKE '%MALE%' THEN 1 ELSE 0 END as gender_male
,CASE WHEN genders LIKE '%FEMALE%' THEN 1 ELSE 0 END as gender_female
but then I realized that MALE will always be present since it's a substring of FEMALE.
I then tried this
,CASE WHEN 'MALE' in UNNEST(split(genders, ";")) THEN 1 ELSE 0 END as gender_male
,CASE WHEN 'FEMALE' in UNNEST(split(genders, ";")) THEN 1 ELSE 0 END as gender_female
but this didn't work either.
I'm using BigQuery btw.

Use below instead
,CASE WHEN REGEXP_CONTAINS(genders, r'\bMALE\b') THEN 1 ELSE 0 END as gender_male
,CASE WHEN REGEXP_CONTAINS(genders, r'\bFEMALE\b') THEN 1 ELSE 0 END as gender_female
or
,IF(REGEXP_CONTAINS(genders, r'\bMALE\b'), 1, 0) as gender_male
,IF(REGEXP_CONTAINS(genders, r'\bFEMALE\b'), 1, 0) as gender_female
both with the output (if apply to sample data in your question)

Related

IF result is IN ('m','f') THEN PRINT (tsql)

There is a 'gender' field in Member table that has either 'm' or 'f' as values. I want to PRINT 'PASS' if both 'm' and 'f' exist in the field and PRINT 'FAIL' if:
only one of two values(m or f) exists
or 2. value other than m or f exists
or 3. null record exists.
When I run the following code, I get "Subquery returned more than 1 value" message.
IF ((SELECT DISTINCT Gender FROM dbo.Member) in ('M','F'))
PRINT 'PASS'
ELSE
PRINT 'FAIL'`
Thank you in advance!
IF EXISTS (SELECT * FROM (
SELECT sum(case when gender= 'M' then 1 else 0 end) M,
sum(case when gender= 'F' then 1 else 0 end) F,
sum(case when gender not in('F', 'M') then 1 else 0 end ) Other
FROM dbo.Member) a
WHERE a.M>0 and a.F>0 and a.Other=0)
PRINT 'PASS'
ELSE
PRINT 'FAIL'

My Sql print statement is not workinng

How do I get this to work? If the count is higher for singles i would like it to output yes and then no for viceversa.
IF
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "M"
<
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "S"
Print 'Yes'
ELSE
Print 'No';
You can't use IF inside a query, instead use a CASE expression with conditional aggregation:
SELECT
CASE WHEN SUM(CASE WHEN StudMaritalStatus = 'M' THEN 1 ELSE 0 END) <
SUM(CASE WHEN StudMaritalStatus = 'S' THEN 1 ELSE 0 END)
THEN 'Yes' ELSE 'No' END AS label
FROM students

How to use sum(case) with three conditions

I usually use sum(case) to get sum of some columns:
i.e. SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05
and this is perfect when I have a column with two values, but when I have a column where I have three values:
i.e. over 05 = '1' or 'X' or '2'
how can I do a sum(case)?
If you want all three values to return the same thing, you should use IN():
SUM(
CASE
WHEN over05 IN ('1', 'X', '2') THEN 1
ELSE 0 END
) AS OK_05
If you want each value to return something different, you should use multiple WHEN ... THEN :
SUM(
CASE
WHEN over05 = '1' THEN 1
WHEN over05 = 'X' THEN 2
WHEN over05 = '2' THEN 3
ELSE 0 END
) AS OK_05

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

Sql Nested group by

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;