I am trying to create an SQL query which returns a Business name. Employees are members of Teams AND of Groups, Teams and Groups are members of Businesses.
I need to select the Business which has employees which are a members of a Team, which has certain attributes as a whole.
How do I go about writing SQL to return the Businesses which have a member to senior member ratio of %? ex Team A has only 20 senior employees out of 100 team members; so it fails and the Business which Team A belongs to should be returned.
I know I need to group Employees together based on their Team, but somehow need to count the Employees who have SENIOR=YES against SENIOR=NO AND return the business they belong to. Ideas?
They are MS Access tables which contain some sensitive information, but you can think of the tables as being set up as follow:
tblBusiness: BID, BName
tblTeam: TID, TName, TBusRef
tblEmployee: EID, EName, ETeamRef, EBusRef, ESenior
I’ve literally tried all manner of SQL: I’ve done everything from nested SQL to heavy aggregation to expressions containing count. The closest (I think) I’ve gotten is:
SELECT tblBuisiness.BName FROM tblBuisiness
GROUP BY BName HAVING (
COUNT(SELECT DISTINCT tblEmployee.ETeamRef FROM tblEmployee WHERE ESenior=-1)
/
COUNT(SELECT DISTINCT tblEmployee.ETeamRef FROM tblEmployee)
< 0.5
)
Now this one might have syntax errors (was on the clipboard) but I went through it 100x and the closest I got was “cannot have expression in agregate “COUNT()/<0.5” “
If TeamA of Business1 has 20 senior members and 80 regular members, then this query is to return Business1.
Perhaps: