Using CASE statement along with SELECT in SQLite to exclude displaying certain results - sql

I was trying to solve the problem of "The number of people of each gender there are in the database, where the gender is known, display the count of people against each gender" in SQLite. I typed a query
SELECT sex, COUNT(name) from employee_table GROUP BY sex ORDER BY sex DESC;
which is partially correct as it showed as output the number of males and females. But, it also shows the number of blank entries which were neither classified as Male or Female.
I have to stop displaying the count of the blank entries. I guess for this, I have to use a CASE statement. I tried CASE statement in different ways. But those didn't work. I tried searching the internet and stack overflow and tried those methods. Those also didn't work.
Any help would be appreciated.
Thanks,
Richard

You want to filter the results and a case expression doesn't do that. Without sample data, it is difficult to say exactly what the expression is, but perhaps:
SELECT sex, COUNT(name)
FROM employee_table
WHERE sex IS NOT NULL
GROUP BY sex
ORDER BY sex DESC;
If the "blank" results are really empty strings, then that would be:
WHERE sex <> ''
This also filters out NULL values.
Or if you want explicit values, then you can use IN:
WHERE sex IN ('m', 'f')

Related

Access SQL Can't Create Two Grouped Averages

Apologies for my simple problem, I am an absolute novice. I have the following code in separate queries
I am attempting to display 3 columns, the average male salary for a set job, average female salary for a set job and the JobID. Separately these queries work however I cannot work out how to combine them.
I have tried multiple solutions from this site for example trying to put multiple select statements inside
and also by using a 'union' solution however cannot get either to work.union This simply compiles them into a single column and sorts via salary not JobID.
SELECT Round(Avg(Salary)) AS AverageMaleSalary, JobID
FROM Employee WHERE Gender = "M"
GROUP BY JobID;
SELECT Round(Avg(Salary)) AS AverageFemaleSalary, JobID
FROM Employee WHERE Gender = "F"
GROUP BY JobID;
You could use conditional aggregation
SELECT JobId,ROUND(AVG(IIF(Gender='F', Salary, NULL))) AS AverageFemaleSalary
,ROUND(AVG(IIF(Gender='M', Salary, NULL))) AS AverageMaleSalary
FROM Employee
GROUP BY JobId;

SQL display MAX value not displaying correctly

I need to display the highest height, first name,and last name of a patient from my table where gender is female. The issues is it is not displaying the highest height. It shows all Females. What am I doing wrong?
SELECT MAX(PatientHeight) as PatientHeight, FirstName, LastName
FROM Patients
WHERE Gender = 'F'
GROUP BY FirstName, LastName
Edit:
I am adding the solution incase someone find this and wants to know more. This was back when I was just learning SQL and had no one I could ask so I posted it here. I want to thank User Jon Ekiz For briefly explaining and better way of approaching the problem. His solution below was exactly was I need to better understand SQL
You can't group by first name and last name and expect just one result. Use this instead:
select firstname
, lastname
, patientheight
from patients
where patientheight = (
select max(patientheight) max_height
from patients
where gender = 'F'
)
and gender = 'F'
BEtter solution is to use row_number and get the first result but not sure which dbms you are using.
Your question isn't clear enough, but following query should return what you're looking for. Question: What if multiple females have same height and are the tallest?
SELECT top 1 PatientHeight, FirstName, LastName
FROM Patients
WHERE Gender = 'F'
Order by PatientHeight desc
Does this do it?
SELECT TOP 1 PatientHeight,FirstName,LastName
FROM Patients
WHERE Gender = 'F'
ORDER BY PatientHeight DESC

SQL: A Count that merges/joins two columns

A company wants to know what has happened since the beginning of 1999 on hiring. The human resources manager has asked you to
produce a count of the employees hired since then, broken down by both age and gender simultaneously (i.e. 17 males, 25 females, etc.). Write a query that does that.
This is what I have so far. I couldn't figure out how to merge two columns simultaneously. Any thoughts?
SELECT EmployeeID, COUNT(*) AS "Number of employees"
FROM Employee
WHERE Age and Gender
GROUP BY EmployeeID
HAVING COUNT(*) BEGIN = 1999
I am not pretty much sure about your requirement but the right way to write a query is as following and this may fulfill your requirement with slight changes...
SELECT Gender, COUNT(*) AS "Number of employees", Age
FROM Employee
WHERE year(column_name) >= 1999 --Give a real date column here
GROUP BY Age, Gender
Please provide table structure and complete desired output for the exact answer.

SQL Server filtering

A new government reporting regulation requires you to develop a query that can count the number of male dependents of employees of the company. The information is stored in the dep_gender column of the dependent table. The result table should have a single output column labeled Number Male Dependents.
So for this part I have :
SELECT COUNT(DEP_GENDER)"Number of Male Dependents"
FROM dependent
where dep_gender = 'M';
which works just fine now to 'revise' for the next part.. I am having some problems because I cannot figure out how to filter into two different groups with associated counts for males and females.
A revision to the government reporting regulation requires the report to count the number of male and female dependents of the company. Display the information as two columns, one for gender and one for the associated count. The result table should have two rows, one for each gender. Use a single query. Additionally, the gender output column should be formatted as CHAR(6) and have a heading label of Gender. The count column should have a heading label of Number Counted.
I think you're looking for a Group By query:
Select Cast(Dep_Gender as Char(6)) as Gender,
Count(1) 'Number Counted'
From Dependent
Group By Cast(Dep_Gender as Char(6))
Cast is used since the requirements (homework perhaps) are to format the gender as a char(6).
SELECT 'Male' As [Gender], COUNT(DEP_GENDER) AS [Counted]
FROM dependent
where dep_gender = 'M'
UNION ALL
SELECT 'Female', COUNT(DEP_GENDER)
FROM dependent
where dep_gender = 'F'

Geting value count from an Oracle Table

I have a table, that contains employees. Since the company I'm working for is quite big (>3k employees) It is only natural, that some of them have the same names. Now they can be differentiated by their usernames, but since a webpage needs a drop-down with all of these users, I need to add some extra data to their names.
I know I could first grab all of the users and then run them through a foreach and add a count to each of the user objects. That would be quite ineffective though. Therefore I'm in need of a good SQL query, that would do something like this. Could a sub-query be the thing I need?
My Table looks something like this:
name ----- surname ----- username
John Mayer jmaye
Suzan Harvey sharv
John Mayer jmay3
Now what I think would be great, if the query returned the same 3 fields and also a boolean if there is more than one person with the same name and surname combination.
Adding the flag to Daniel's answer...
SELECT NAME, SURNAME, USERNAME, DECODE(COUNT(*) OVER (PARTITION BY NAME, SURNAME), 1, 'N', 'Y')
FROM
YOUR_TABLE;
Please note that Oracle SQL has no support for booleans (sigh...)
This can be easily done with a count over partition:
SELECT NAME, SURNAME, USERNAME, COUNT(*) OVER (PARTITION BY NAME, SURNAME)
FROM
YOUR_TABLE;