SQL query to retrieve average of different aggregation type in single query - sql

This is my data model:
I need to get name, surname and personal votes average of all students in classroom that have the average lower then the votes average of all students in classroom. Following the desired output:
+---------+------+--------+-------------+---------------+
| surname | name | class | class_avg | studend_avg |
+---------+------+--------+-------------+---------------+
| b | b | 1 | 4.1250 | 2.7500 |
+---------+------+--------+-------------+---------------+
I wrote following query that works correctly in mariadb database:
SELECT student.surname, student.name, student.classroom, classroom.average AS classroom_average, AVG(vote.vote) AS student_average
FROM (student INNER JOIN vote ON student.fiscalcode = vote.fiscalcode)
INNER JOIN
(select student.classroom AS classroom, AVG(vote.vote) AS average
FROM student INNER JOIN vote ON student.fiscalcode = vote.fiscalcode
GROUP BY student.classroom) AS classroom
ON student.classroom=classroom.classroom
GROUP BY student.surname, student.name, student.classroom, classroom.average
HAVING AVG(vote.vote) < classroom.average;
But I have this error in MS Access:
Your Query does not include the specified expression "AVG(vote.vote)/AVG(vote.vote) < classroom.average" as part of an aggregate function
There are some other easier method to write this query?

Consider:
SELECT StudentAvg.fiscalcode, student.name, student.surname, StudentAvg.AvgOfvote, ClassAvg.AvgOfvote
FROM ((SELECT student.classroom, student.fiscalcode, Avg(vote.vote) AS AvgOfvote
FROM student INNER JOIN vote ON student.fiscalcode = vote.fiscalcode
GROUP BY student.classroom, student.fiscalcode) As StudentAvg INNER JOIN (SELECT student.classroom, Avg(vote.vote) AS AvgOfvote
FROM vote INNER JOIN student ON vote.fiscalcode = student.fiscalcode
GROUP BY student.classroom) AS ClassAvg ON StudentAvg.classroom = ClassAvg.classroom) INNER JOIN student ON StudentAvg.fiscalcode = student.fiscalcode
WHERE (((StudentAvg.AvgOfvote)<[ClassAvg]![AvgOfvote]));

Related

Access SQL JOIN with subquery

Can someone tell me how I can convert this query to work in MS Access?
SELECT Shooters.idShooters, Shooters.firstname, Shooters.lastname, JoinedContingent.Count, JoinedShots.Count
FROM Shooters
INNER JOIN
(SELECT Shooters.idShooters, Count(Contingent.idContingent) AS Count
FROM Shooters LEFT JOIN Contingent ON Shooters.idShooters = Contingent.fidShooter
GROUP BY Shooters.idShooters)
AS JoinedContingent ON JoinedContingent.idShooters = Shooters.idShooters
INNER JOIN
(SELECT Shooters.idShooters, Count(Shots.idShots) AS Count
FROM Shooters LEFT JOIN Shots ON Shooters.idShooters = Shots.fidShooters
GROUP BY Shooters.idShooters)
AS JoinedShots ON JoinedShots.idShooters = Shooters.idShooters;
Background information:
I would like to count the foreign key occurrences for each Shooter in the table Contingent and Shots. The result should look like this:
idShooters | firstname | lastname | Count | Count
____________________________________________________________
1 John Doe 0 10
2 Jane Doe 1 20
.
.
.
I think this is what you want:
SELECT s.idShooters, s.firstname, s.lastname,
NZ(c.Count, 0), NZ(sh.Count, 0)
FROM (Shooters as s LEFT JOIN
(SELECT c.fidShooter, Count(*) AS Count
FROM Contingent as c
GROUP BY c.fidShooter
) as c
ON s.idShooters = c.fidShooter
) LEFT JOIN
(SELECT sh.fidShooters, Count(*) AS Count
FROM Shots as sh
GROUP BY sh.fidShooters
) as sh
ON s.idShooters = sh.fidShooters;
Note that I moved the outer join to the outer query. Actually, no joins are needed in the subqueries, so don't bother.

Query sql to get the first occurrence in a many to many relationship

I have a User table that has a many to many relationship with Areas. This relationship is stored in the Rel_User_area table. I want to show the user name and the first area that appears in the list of areas.
Ex.
User
id | Name
1 | Peter
2 | Joe
Area
id | Name
1 | Area A
2 | Area B
3 | Area C
Rel_User_area
iduser | idarea
1 | 1
1 | 3
2 | 3
The result I want:
User Name | Area
Peter |Area A
Joe |Area C
Using the minimum area id to determine "First" you could use a correlated subquery (A subquery that refers to field(s) in the main query to filter results):
SELECT user.name, area.name
FROM
user
INNER JOIN Rel_User_Area RUA ON user.id = RUA.iduser
INNER JOIN Area ON RUA.idarea = area.id
WHERE area.id = (SELECT min(idarea) FROM Rel_User_Area WHERE iduser = RUA.iduser)
There's other ways of doing this that may be RDBMS specific. Like in Teradata I would use a QUALIFY clause that doesn't exist in MySQL, SQL Server, Oracle, Postgres, etc.. Regardless of the RDBMS the above should work.
SELECT user.name, area.name
FROM
user
INNER JOIN Rel_User_Area RUA ON user.id = RUA.iduser
INNER JOIN Area ON RUA.idarea = area.id
QUALIFY ROW_NUMBER() OVER (PARTITION BY user.id ORDER BY area.id ASC) = 1;
using the ID from Rel_user_Area you mentioned in comments...
This should be pretty platform independent.
SELECT U.name as Username, A.Name as Area
FROM (SELECT min(ID) minID, IDUser, IDarea
FROM Rel_user_Area
GROUP BY IDUser, IDarea) UA
INNER JOIN User U
on U.ID = UA.IDuser
INNER JOIN Area A
on A.ID = UA.IDArea
If Cross apply and top work (could substitute limit 1 vs top if Postgresql or mySQL)
This will run the cross apply SQL once for each record in user; thus you get the most recent rel_user_Area ID per user.
SELECT U.name as Username, A.Name as Area
FROM User U
on U.ID = UA.IDuser
CROSS APPLY (SELECT TOP 1 IDUser, IDArea
FROM Rel_user_Area z
WHERE Z.IDUSER = U.ID
ORDER BY ID ASC) UA
INNER JOIN Area A
on A.ID = UA.IDArea

SQL query using NULL

I have two tables, student and school.
student
stid | stname | schid | status
school
schid | schname
Status can be many things for temporary students, but NULL for permanent students.
How do I list names of schools which has no temporary students?
Using Conditional Aggregate you can count the number of permanent student in each school.
If total count of a school is same as the conditional count of a school then the school does not have any temporary students.
Using JOIN
SELECT sc.schid,
sc.schname
FROM student s
JOIN school sc
ON s.schid = sc.schid
GROUP BY sc.schid,
sc.schname
HAVING( CASE WHEN status IS NULL THEN 1 END ) = Count(*)
Another way using EXISTS
SELECT sc.schid,
sc.schname
FROM school sc
WHERE EXISTS (SELECT 1
FROM student s
WHERE s.schid = sc.schid
HAVING( CASE WHEN status IS NULL THEN 1 END ) = Count(*))
You can use not exists to only select schools that do not have temporary students:
select * from school s
where not exists (
select 1 from student s2
where s2.schid = s.schid
and s2.status is not null
)
You can use a regular join.
SELECT DISTINCT c.schName
FROM Students s
INNER JOIN Schools c ON s.schid = c.schid
WHERE s.status IS NULL

Oracle SQL join three tables and group by column

I have three tables and I want a query te select teacher names and the number of classes each teacher has reserved.
teacher:
| idt | name |
class:
| idc | name |
reserve:
| idc | idt |
My query:
select
t.name, count(distinct(r.idc))
from
teacher t
join
reserve r
on
r.idt = t.idt
join
class c
on
c.idc = r.idc
group by r.idc
When I run this I get the followin error: not a group by expression.
The group by clause needs to contain all non-aggregated columns from the select statement; in your case it should be t.name. Also, distinct is not a function but a keyword and should not have parentheses.
select
t.name,
count(distinct r.idc) as number_of_classes
from
teacher t
join
reserve r on r.idt = t.idt
join
class c on c.idc = r.idc
group by
t.name

SQL: tricky join

I'm building software for exam signup and grading:
I need to get data from these two tables:
Exams
|------------------------------------------------------------|
| ExamId | ExamTitle | EducationId | ExamDate |
|------------------------------------------------------------|
ExamAttempts
|-----------------------------------------------------------------------------|
| ExamAttemptId | ExamId | StudentId | Grade | NotPresentCode |
|-----------------------------------------------------------------------------|
Students attends an education
Educations have multiple exams
Students have up to 6 attempts per exam
Every attempt is graded or marked as not present
Students can sign up for an exam if:
- not passed yet (grade below 2)
- has not used all attempts
I want to list every exams that a student can sign up for.
It maybe fairly simple, but I just can't get my head around it and now I'm stuck! I've tried EVERYTHING but haven't got it right yet. This is one of the more hopeless tries I made (!):
CREATE PROCEDURE getExamsOpenForSignUp
#EducationId int,
#StudentId int
AS
SELECT ex.*
FROM Exams ex
LEFT JOIN (
SELECT ExamId, COUNT(ExamId) AS NumAttempts
FROM ExamAttempts
WHERE StudentId = #StudentId AND grade < 2 OR grade IS NULL
GROUP BY ExamId
) exGrouped ON ex.ExamId = exGrouped.ExamId
WHERE educationid = #EducationId and exGrouped.ExamId IS NULL OR exGrouped.NumAttempts < 6;
GO
What am i doing wrong? Please help...
You need to start with a list of all possibilities of exams and students and then weed out the ones that don't meet the requirements.
select driver.StudentId, driver.ExamId
from (select #StudentId as StudentId, e.ExamId
from exams e
where e.EducationId = #EducationId
) driver left outer join
(select ea.ExamId, ea.StudentId
from ExamAttempts ea
group by ea.ExamId, ea.StudentId
having max(grade) >= 2 or -- passed
count(*) >= 6
) NotEligible
on driver.ExamId = NotEligible.ExamId and
driver.StudentId = NotEligible.StudentId
where NotEligible.ExamId is NULL
The structure of this query is quite specific. The driver table contains all possible combinations. In this case, you have only one student and all exams are in the "education". Then the left join determines which are not eligible, based on your two requirements. The final where is selecting the non-matches to the not-eligible -- or the exams that are eligible.
Check if this works in your SP:
Select EduExams.ExamId
from
(select * from Exams
where Exams.EducationId = #EducationId) EduExams
left outer join
(select * from ExamAttempts
where ExamAttempts.StudentId = #StudentId) StudentAttempts
on EduExams.ExamID = StudentAttempts.ExamId
group by EduExams.ExamId
having count(StudentAttempts.ExamAttemptId) < 6
and ((max(StudentAttempts.Grade) is null) or (max(StudentAttempts.Grade) < 2))
OK thank you both for your help - much appreciated!
Based on #Gordon Linoffs solution. this is what I ended up with:
SELECT driver.ExamId, driver.ExamTitle
FROM (
SELECT #StudentId AS StudentId, e.ExamId, e.ExamTitle
FROM exams e
WHERE e.EducationId = #EducationId
) driver
LEFT JOIN (
SELECT ea.ExamId, ea.StudentId
FROM ExamAttempts ea
WHERE ea.studentId = #StudentId
GROUP BY ea.ExamId, ea.StudentId
HAVING MAX(grade) >= 2 OR COUNT(*) >= 6
) NotEligible
ON driver.ExamId = NotEligible.ExamId AND driver.StudentId = NotEligible.StudentId
WHERE NotEligible.ExamId IS NULL