Dick said:
Can someone tell me why this query fails? Access indicates a syntax
error in the FROM clause, but I don't see it.
SELECT ae.AEName, A.COB
FROM ae LEFT JOIN (SELECT invcimpt.[ae#], sum(invcimpt.aecom) as COB
FROM invcimpt GROUP BY invcimpt.[ae#]) AS A ON ae.aeno = A.[ae#]
WHERE (((ae.Status)="x" Or (ae.Status)="O"))
GROUP BY ae.AEName;
I know there is a simpler way to do this particular operation, but
this example is just an element in a more complex problem.
When I tried to run your query in A2003, I got this error:
"You tried to execute a query that does not include the expression 'COB' as
part of an aggregate function"
at which point I went "DOH! Of course!"
Would you expect this query to run without error?
SELECT AEName, aeno
FROM ae
GROUP BY AEName
No, of course this would cause an error: you have to either include aeno in
the GROUP BY clause, or apply an aggregate function to it:
SELECT AEName, max(aeno)
FROM ae
GROUP BY AEName
You problem query can be fixed using the same technique:
SELECT ae.AEName, Max(A.sumCOB) AS [COB]
FROM ae LEFT JOIN
(SELECT invcimpt.[ae#], sum(invcimpt.aecom) as sumCOB
FROM invcimpt GROUP BY invcimpt.[ae#]) AS A ON ae.aeno = A.[ae#]
WHERE (((ae.Status)="x" Or (ae.Status)="O"))
GROUP BY ae.AEName;
You could also eliminate the subquery, but I assume you know this based on
your statement "I know there is a simpler way to do this particular
operation ... "
Now, the only question is: why did you not get the more explicit error
message that I got? Two possibilities come to mind:
1. You are using an earlier version of Access than the one I used
2. You are attempting to execute it via ADO from an external application
without first testing it in the Access environment
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"