Sql getting different AVG from the same select and table - sql

I am working on the northwind database for my sql studies and i`m trying to show How many Employees live in each country and their average age.
This is what i managed to do so far:
select Country,COUNT(EmployeeID) 'AmountOfEmployees',
(select AVG(year (getdate()) - year (birthdate)) from Employees where Employees.Country = 'USA') 'USA_Average_Age',
(select AVG(year (getdate()) - year (birthdate)) from Employees where Employees.Country = 'UK') 'UK_Average_Age'
from Employees
group by Country
and the result is:
I cant seem to manage to get different avgs on the same column for UK and USA. is it even possible?
Hope i was clear with my question.
Would appreciate any help.

Subqueries are not necessary. I may not approve of your calculation for age, but you can use it:
select Country, COUNT(EmployeeID) as NumberOfEmployees,
avg(year(getdate() - year(birthdate)) as average
from Employees
group by Country;
This puts the average on different rows.

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 query in mariadb

I have a task to write a SQL query to find which hospital has the most doctors and for that hospital to show all the patients and dates of their checkup.
Relations that are involved in my db are next:
Hospital (HospitalID, Name_of_the_Hospital, Place, Phone number)
Scheduling_Checkups (Hospital, Doctor, Patient, Date_of_the_checkup)
Patient (PatientID, Name, Addres, Phone_number)
Doctor (DoctorID, Name, Speciality)
I tried this:
SELECT
hospital.Name,COUNT( Scheduling_Checkups.Doctor) AS 'number'
FROM
hospital
JOIN
Scheduling_Checkups ON hospital.HospitalID = Scheduling_Checkups.Hospital
GROUP BY
Hospital.Name, Scheduling_Checkups.Doctor
ORDER BY
number DESC
But this returns only total number of the doctors that appeared in table
Scheduling_Checkups, and if someone appeared twice, it will return incorrect numbers, for example if it said 9 and I had one doctor who appeared twice in the same hospital, then it would be a wrong answer, correct answer would be 8.
Can anyone give me a point and direction how to answer this.
And the query that I tried only tries to find first part.is at all possible to find the hospital with highest number of doctors employeed and in the same query to show patients and dates of their checkups for that same hospital
Your query result with limit 1 could be the where value for set the hopital you need and the join scheduling and patient
select Patient.*, Scheduling_Checkups.*
from Scheduling_Checkups
INNER JOIN Patient on Scheduling_Checkups.Patient = Patient.PatientID
where Scheduling_Checkups.Hospital = (
SELECT hospital.HospitalID
FROM hospital
join Scheduling_Checkups ON hospital.HospitalID= Scheduling_Checkups.Hospital
GROUP BY Hospital.HospitalID, Scheduling_Checkups.Doctor
ORDER BY COUNT( Scheduling_Checkups.Doctor) DESC LIMIT 1
)

Using ROUND, AVG and COUNT in the same SQL query

I need to write a query where I need to first count the people working in a department, then calculate the average people working in a department and finally round it to only one decimal place. I tried so many different variations.
That's what I got so far although it's not the first one I tried but I always get the same error message. (ORA-00979 - not a group by expression)
SELECT department_id,
ROUND(AVG(c.cnumber),1)
FROM employees c
WHERE c.cnumber =
(SELECT COUNT(c.employee_id)
FROM employees c)
GROUP BY department_id;
I really don't know what do to at this point and would appreciate any help.
Employees Table:
Try this (Oracle syntax) example from your description:
with department_count as (
SELECT department_id, COUNT(c.employee_id) as employee_count
FROM employees c
group by department_id
)
SELECT department_id,
ROUND(AVG(c.employee_count),1)
FROM department_count c
GROUP BY department_id;
But this query not make sense. Count is integer, and count return one number for one department in this case AVG return the same value as count.
Maybe you have calculate number of employee and averange of salary on department?

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.

Show only last record with specific field data in access query

My SQL skills are quite basic, but I'm trying to set a database of not-profits for a small business.
I have a table (extract_financial) with financial data:
regno (organisation registration number, unique),
fystart (date, financial year start),
fyend (date, financial year end),
income,
exped (expenditure).
Each organisation will have a few records for different financial years. Not all records include income and expenditure values.
I want to show only one record per organisation (including regno, fyend, income), the latest one which has any income.
I've tried the following script, adapted from a similar question, but it didn't work:
SELECT ef.regno, ef.fyend, ef.income
FROM extract_financial ef
INNER JOIN
(
SELECT regno, Max(fyend) AS MaxOfFyend
FROM extract_financial
GROUP BY regno
) AS efx
ON ef.regno = efx.regno
AND ef.fyend = efx.MaxOfFyend
WHERE ef.income IS NOT NULL
The query to find the latest entry for each [regno] works, but the problem is that the latest record never has any income in it... So I guess I need an IF THEN?
Would appreciate your help, thanks!
You are close. The WHERE clause needs to go in the subquery:
SELECT ef.regno, ef.fyend, ef.income
FROM extract_financial as ef INNER JOIN
(SELECT regno, Max(fyend) AS MaxOfFyend
FROM extract_financial as ef
WHERE ef.income IS NOT NULL
GROUP BY regno
) AS efx
ON ef.regno = efx.regno AND ef.fyend = efx.MaxOfFyend;