Hi Guys, thanks for your replies. I have been away for a couple of days,
hence why i have not come back to you.
I now understand that Allen Browne was correct. You can't alias a join
(unless you save it as a query in it's own right), you can only alias the
tables in the join.
Having got over that point of learning, can I lay the full problem out for
you.
Objects
--------
Org- I have a table of Organisations, storing a single
hierarchy of Orgs.
Submission- Organisations should (but may not) submit a plan.
Programme- Each submission (plan) can contain 0 or more programmes with
names and expected dates for their programme plans to be
submitted.
Query
------
I want a query to list:
1) The organisations.
2) The submission dates.
3) The date and name of the first programme submission (by date) planned by
that org.
It's (3) that's causing me problems.
Note: Because not all Orgs may have a submission I have had to create a
separate query to get around Access' limitations with outer joins. The query
"_OJ_Org_Submission" lists all Orgs and their Submissions.
Attempt 1
----------
SELECT OS.*
, (SELECT * FROM Programme AS P
WHERE P.SubmissionID = OS.SubmissionID
AND P.DateSchedSubmit = Min(P.DateSchedSubmit))
FROM _OJ_Org_Submission AS OS
This fails with the error "Cannot have an aggregate function in a WHERE
clause".
This surprises me because i'm pretty sure this syntax works in multiple
other DBMS'.
Attempt 2
----------
SELECT OS.*
, (SELECT TOP 1 * FROM Programme AS P
WHERE P.SubmissionID = OS.SubmissionID
ORDER BY DateSchedSubmit)
FROM _OJ_Org_Submission AS OS
Fails with the error "You have written a subquery that can return more than
one field..."
Only one field per subquery? This could get painful. Does it take 10
subqueries (with identical WHERE clauses) to return 10 fields?
Attempt 3 (just the Programme Name)
----------
SELECT OS.*
, (SELECT TOP 1 ProgName FROM Programme AS P
WHERE P.SubmissionID = OS.SubmissionID
ORDER BY DateSchedSubmit)
FROM _OJ_Org_Submission AS OS
Fails with the error "At most one record can be returned by this subquery".
1) Isn't that exactly what the "TOP 1" clause means?
2) I thought the "TOP 1" and "Order By" combination was the standard way in
Access of constructing the equivalent of a "Where Date=Max(Date)" type
Select query.
Any ideas on how to approach this?
Regards: Colin