mysqlsqljoingroup-byhaving-clause

SQL Query with multiple conditions not working


Okay so this is my best attempt at making a query. It currently executes but returns nothing the other attempts have not worked.

The joins are wrong I believe and I'm not sure how to fix them or do this right

In this query I am trying to get the activity details with staff assigned to those activities matching all conditions below.

The staff and activities are linked in the Allocation table with a Staff ID assigned to an Activity ID there.

--Teach at least 2 modules during 2019 in a certain campus

The staffID must occur at least twice in the Teach table at the field StaffID

The spaID 5 or 6 would correspond to the campus so teach two modules at this time and ID.

--Supervise more than 1 colleagues

This is in the staff table with SupervisorID being a foreign key of StaffID in the same table.

So the Staff ID allocated to the activity needs to supervise more than one colleague with their ID appearing in SupervisorID more than once.

--The related activity is internal one

The activity is internal if it equals to 1.

So to sum it up the activity details should only appear in the query results if it has staff matching all the conditions above

SELECT Activity.AcID, Activity.Title, Activity.CaID, Activity.Internal, Activity.BuID, Budget.Amount FROM Activity


INNER JOIN Budget ON Activity.AcID = Budget.BuID
INNER JOIN Allocation ON Activity.AcID = Allocation.AcID
INNER JOIN Staff ON Allocation.StaffID = Staff.StaffID
INNER JOIN Teach ON Allocation.StaffID = Teach.StaffID

WHERE Activity.Internal=1 AND 

Allocation.StaffID IN (
SELECT Staff.SupervisorID
FROM Staff
GROUP BY StaffID
HAVING COUNT(Staff.SupervisorID=Allocation.StaffID) >1) 
 
AND Allocation.StaffID IN (
    
SELECT Teach.StaffID
FROM Teach
WHERE Teach.Year='2019' AND Teach.SpaID=5 OR 6
GROUP BY Teach.StaffID
HAVING COUNT(Allocation.StaffID=Teach.StaffID) >=2);

Table details are below if you want to look at them for more details

Table Activity -- AcID, Title, CaID, BuID, Status, Started, Ended Internal

Table Allocation -- StaffID, AcID

Table Budget -- BuID, Amount, Approver, Payee, Status

Table Campus -- CaID, Address, GmName, Country, Status

Table Classroom -- RmID, Capacity, CaID, Location, Type, Status

Table Module -- ModuleID, Module Name, DeptID, Programme, TMode, Date_of_Firstoffer

Table SpaceAssign -- SpaID, RmID, TID, Manager, Approved

Table Staff -- StaffID, Title, FirstName, LastName, DeptID, CaID, Joined, LeftD, Current, Salary, ContractType, SupervisorID

Table Teach -- TID, ModuleID, StaffID, SpaID, Semester, Year

I have tried my best to explain this well sorry for any confusion.


Solution

  • Without data It's very tough to find a solution. Please try this:

    SELECT Activity.AcID, Activity.Title, Activity.CaID, Activity.Internal, Activity.BuID, Budget.Amount FROM Activity
    
    
    INNER JOIN Budget ON Activity.AcID = Budget.BuID
    INNER JOIN Allocation ON Activity.AcID = Allocation.AcID
    INNER JOIN Staff ON Allocation.StaffID = Staff.StaffID
    INNER JOIN Teach ON Allocation.StaffID = Teach.StaffID
    
    WHERE Activity.Internal=1 AND 
    
    Allocation.StaffID IN (
    SELECT Staff.SupervisorID
    FROM Staff
    GROUP BY SupervisorID
    HAVING COUNT(*) >1) 
     
    AND Allocation.StaffID IN (
        
    SELECT Teach.StaffID
    FROM Teach
    WHERE Teach.Year='2019' AND Teach.SpaID=5 OR 6
    GROUP BY Teach.StaffID
    HAVING COUNT(*) >=2);