Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

Need help forming a complicated query

I have a report that I need to generate that pulls data from 2 tables: employer and employee.  The rows of the report are the employers and the columns are the Active Employees, Paid Out Employees and Forfeiture Employees.  The column data is pulled from the Employees table and is dependent upon counting employee records based on 2 different fields in the employee table: EmployeeStatus = 'ACTIVE' for the Active Column, PlanStatus='PAID OUT' for the Paid Out column and PlanStatus='FORFEITURE' for the Forfeiture column.  I created 3 separate queries to display the data I need with the help of an expert in this forum.  I thought it would be a simple matter to join the 3 queries to get the end results that I need but I'm getting an error.  Below is my query which attempts to display the Active and Paid Out Employee totals.

If I remove the c.PaidOutEmployees and the corresponding JOIN, the query displays the employers and the count of active employees.  When I try to add the PaidOutEmployees Query I get the following errror:

Syntax error (missing operator) in query expression
'a.EmployerID=b.EmployerID
LEFT JOIN
(SELECT Count(Employees.PlanStatus) AS PaidOutEmployees, Employees.EmployerID
FROM Employees
WHERE Employees.PlanStatus='PAID OUT'
GROUP BY Employees.EmployerID, Employees.PlanStatus) c ON b.EmployerID = c.Employer'.

Any help would be greatly appreciated.
SELECT a.EmployerName, a.EmployerID, b.ActiveEmployees, c.PaidOutEmployees
FROM Employers a
LEFT JOIN
(SELECT Count(Employees.EmployeeStatus) AS ActiveEmployees, Employees.EmployerID
FROM Employees
WHERE Employees.EmployeeStatus='ACTIVE'
GROUP BY Employees.EmployerID, Employees.EmployeeStatus) b ON a.EmployerID=b.EmployerID
LEFT JOIN
(SELECT Count(Employees.PlanStatus) AS PaidOutEmployees, Employees.EmployerID
FROM Employees
WHERE Employees.PlanStatus='PAID OUT'
GROUP BY Employees.EmployerID, Employees.PlanStatus) c ON b.EmployerID = c.EmployerID;

Open in new window

Avatar of usachrisk1983
usachrisk1983
Flag of United States of America image

I'm not sure this will fix a Syntax error, I'll keep looking at the SQL, but you should probably change"

ON b.EmployerID = c.EmployerID;

to

ON a.EmployerID = c.EmployerID;

I don't know what the source tables look like, but I think the way you have it written now, if there are no active employees for an employer, it wouldn't find the paid out employee.
Avatar of dyarosh
dyarosh

ASKER

I had tried that and received the same error.
I created a mockup of your database as follows:

create table employers (employername varchar(200), employerid numeric(1));
create table employees (employeestatus varchar(200), planstatus varchar(200), employerid numeric(1));

insert into employers (employername, employerid) values ('Some Employer',1);

insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','NOT PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','NOT PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','NOT PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','NOT PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','NOT PAID OUT',1);

insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','PAID OUT',1);
insert into employees (employeestatus, planstatus, employerid) values ('ACTIVE','PAID OUT',1);

I then ran your query on MS SQL 2000 and it ran without any issue (unmodified).  What database are you using?  Could there be some invisible null character or something in there from wherever you copied the SQL from?  Try copying and pasting from ExpertsExchange back into wherever your query is going to see it that works.

Also, some applications don't like the semicolan at the end of the SQL statement (ColdFusion for example), try removing that and seeing if it helps.  Also, if you're using SQL Query Analyzer, put your query in a window of its own to make sure it's the only thing being executed.
Avatar of dyarosh

ASKER

My apologies for not mentioning it is an MS Access database that the query is being applied to.
ASKER CERTIFIED SOLUTION
Avatar of usachrisk1983
usachrisk1983
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh

ASKER

My query now looks like this and I still get the error.  Also, the link above gave me page cannot be found.
 
SELECT a.EmployerName, a.EmployerID, b.ActiveEmployees, c.PaidOutEmployees
FROM (Employers a
LEFT JOIN
(SELECT Count(Employees.EmployeeStatus) AS ActiveEmployees, Employees.EmployerID
FROM Employees
WHERE Employees.EmployeeStatus='ACTIVE'
GROUP BY Employees.EmployerID, Employees.EmployeeStatus) b ON a.EmployerID=b.EmployerID
LEFT JOIN
(SELECT Count(Employees.PlanStatus) AS PaidOutEmployees, Employees.EmployerID
FROM Employees
WHERE Employees.PlanStatus='PAID OUT'
GROUP BY Employees.EmployerID, Employees.PlanStatus) c ON a.EmployerID = c.EmployerID);
The link is:
http://nm1m.blogspot.com/2007/10/multiple-left-joins-in-ms-access.html

I typoed a "3" at the end.

You're query isn't working because you didn't copy & paste the one from my post.  You're missing an end paren after "b ON a.EmployerID=b.EmployerID" and you appear to have added one to the end, before the semicolan, that's not in my post.
Avatar of dyarosh

ASKER

Thank you so much.  I'm now going to try and modify the query one more time to add a third Left Join.  Wish me luck.