Solved

Need help forming a complicated query

Posted on 2009-05-04
8
174 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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