Solved

Need help forming a complicated query

Posted on 2009-05-04
8
173 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24296545
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.
0
 

Author Comment

by:dyarosh
ID: 24296605
I had tried that and received the same error.
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24296616
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dyarosh
ID: 24296691
My apologies for not mentioning it is an MS Access database that the query is being applied to.
0
 
LVL 13

Accepted Solution

by:
usachrisk1983 earned 500 total points
ID: 24296937
Welcome to one of the pain points of Access :)  You need to use another set of parens, for whatever reason, like so:

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;

See a comment on it here:
http://nm1m.blogspot.com/2007/10/multiple-left-joins-in-ms-access.html3


0
 

Author Comment

by:dyarosh
ID: 24297209
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);
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 24297746
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.
0
 

Author Closing Comment

by:dyarosh
ID: 31577600
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question