Solved

Need help forming a complicated query

Posted on 2009-05-04
8
167 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms sql stored procedure 22 87
SQL query bug 3 28
Strange msg in the SSMS pane 13 48
xpath sql query 2008 8 42
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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

20 Experts available now in Live!

Get 1:1 Help Now