Solved

Need help forming a complicated query

Posted on 2009-05-04
8
166 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
  • 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now