Re: Bizarre subquery syntax problem
- From: "Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
- Date: Fri, 16 May 2008 21:52:40 -0400
With Access, the official syntax for a subquery always requires an alias, so
you must write:
SELECT Q.*
FROM (
SELECT p.[User id] AS UID, p.Firstname AS Firstname, p.Lastname AS Lastname,
p.Email AS Email, p.[Profiled member] AS Prof , Left(p.[Date updated],10) AS
Updated, Left(a.[Date last access],10) AS Accessed, (SELECT COUNT(*) FROM
[Users Profile (Detail)] AS q WHERE p.[Lastname] = q.[Lastname] AND
p.[Firstname] = q.[Firstname]) AS Num
FROM [Users Profile (Detail)] AS p LEFT JOIN [Users Address (detail)] AS a
ON p.[User id]=a.[User id]
) as Q;
Also, Access uses [ ... ]. and not the parenthesis () as the delimiter for a
subquery (notice the point . after the right bracket ] ). However, if you
uses parenthesis, Access will understand it. Sometime, it will rewrite
(replace) them with []. and sometime, it will not.
Finally, as you have noticed, Access is very weak at understanding queries
with multiple Left and Right Outer Join and with Subqueries; especially but
not necessarily if you mix both. There is no point of coming here to say
that Access doesn't understanding one of your queries, rewrites it
(correctly or - worse - incorrectly) or even simply choose to core-dump when
you try to execute it.
In some cases, explicitely dividing a big query into multiples queries in
the Queries collection might help but the only thing to do might be often to
use temporary tables or to use simpler queries in association with DLookup()
and DCount() on the form or the report to complete the result.
--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)
"colin_e" <coline@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D054B33D-70F7-43BF-8A98-B20589E99C58@xxxxxxxxxxxxxxxx
Guys, just back to Access after some time away (and I wasn't much good the
first time...)
I am seeing bizarre problems with a subquery.
1) I am joining two tables of user account data on a "User id" field. The
core query runs fine-
SELECT p.[User id] AS UID, p.Firstname AS Firstname, p.Lastname AS
Lastname,
p.Email AS Email, p.[Profiled member] AS Prof , Left(p.[Date updated],10)
AS
Updated, Left(a.[Date last access],10) AS Accessed, (SELECT COUNT(*) FROM
[Users Profile (Detail)] AS q WHERE p.[Lastname] = q.[Lastname] AND
p.[Firstname] = q.[Firstname]) AS Num
FROM [Users Profile (Detail)] AS p LEFT JOIN [Users Address (detail)] AS a
ON p.[User id]=a.[User id];
2) Now, of a query "Q" works, then wrapping it in an outer query-
SELECT * FROM( Q );
must also work, by definition, no?
However this-
SELECT *
FROM (
SELECT p.[User id] AS UID, p.Firstname AS Firstname, p.Lastname AS
Lastname,
p.Email AS Email, p.[Profiled member] AS Prof , Left(p.[Date updated],10)
AS
Updated, Left(a.[Date last access],10) AS Accessed, (SELECT COUNT(*) FROM
[Users Profile (Detail)] AS q WHERE p.[Lastname] = q.[Lastname] AND
p.[Firstname] = q.[Firstname]) AS Num
FROM [Users Profile (Detail)] AS p LEFT JOIN [Users Address (detail)] AS a
ON p.[User id]=a.[User id]
);
fails with an error "syntax error in FROM clause".
3) Now it gets really interesting. By trying every combination of
adding/deleting spaces and carriage returns to the query, I actually got a
slightly more complex version (with an outer ORDER BY) clause to run.
When I closed and reopened this query in Access, two things happened-
3.1) Access had changed the syntax to-
SELECT *
FROM [SELECT p.[User id] AS UID, p.Firstname AS Firstname, p.Lastname AS
Lastname, p.Email AS Email, p.[Profiled member] AS Prof
, Left(p.[Date updated],10) AS Updated, Left(a.[Date last
access],10) AS Accessed
, (SELECT COUNT(*) FROM [Users Profile (Detail)] AS q WHERE
p.[Lastname] = q.[Lastname] AND p.[Firstname] = q.[Firstname]) AS Num
FROM [Users Profile (Detail)] AS p LEFT JOIN [Users Address (detail)]
AS
a ON p.[User id]=a.[User id]
]. AS [%$##@_Alias]
ORDER BY Num DESC , Lastname, Firstname;
Notice the square brackets around the subquery, with a "].", and what the
heck is that "%$##@_Alias" construct?
3.2) It's almost impossible to edit! If I edit this resulting query in any
way, even by deleting a space character, then reinserting the same space,
Access decides the query has a syntax error and refuses to run it. I'm
slightly nonplussed, I though SQL was as insensitive to whitespace as
HTML, C
etc?
Any ideas on what's going on, and how to deal with it, much appreciated.
--
Regards: Colin
.
- Follow-Ups:
- Re: Bizarre subquery syntax problem
- From: colin_e
- Re: Bizarre subquery syntax problem
- From: John Spencer
- Re: Bizarre subquery syntax problem
- References:
- Bizarre subquery syntax problem
- From: colin_e
- Bizarre subquery syntax problem
- Prev by Date: Re: Excluding certain items
- Next by Date: Re: Moving data from one table to another
- Previous by thread: Bizarre subquery syntax problem
- Next by thread: Re: Bizarre subquery syntax problem
- Index(es):
Relevant Pages
|