Subject | Re: [ib-support] SQL syntax problem |
---|---|
Author | Ann W. Harrison |
Post date | 2001-09-15T18:15:51Z |
At 04:35 PM 9/15/2001 +0200, Yves Glodt wrote:
According to the standard, a query that includes a grouping expressing
(i.e. GROUP BY) can retrieve columns listed in the grouping expression
(i.e. CATEGORY) and aggregates of other columns. This makes a certain
amount of sense if you think about a query like this:
select state, city from states group by state;
There are lots of cities in most states. Which is the right one to
return?
Now you know that for each CATEGORY value from ADDRESSES there is exactly
one CATEGORIES record, so, to you, your query makes sense. The SQL
committee doesn't trust you to make that sort of decision, so it requires
you to express your query like this:
or this:
spaces, or the aliasing, those are all my prejudices.)
Regards,
Ann
www.ibphoenix.com
We have answers.
>SELECT ADDRESS.CATEGORY, COUNT(ADDRESS.KEYINDEX), CATEGORIES.NAMEActually, that's mysql playing a bit fast and loose with the SQL standard.
> FROM ADDRESS,CATEGORIES
> WHERE ADDRESS.CATEGORY=CATEGORIES.C_NUMBER
> GROUP BY CATEGORY;
>
>
>This is the resulting error:
>-------------------------------
>Statement failed, SQLCODE = -104
>Dynamic SQL Error
>-SQL error code = -104
>-invalid column reference
>
>
>I've tried *exactly* this query with the same db on mysql, and I got a
>result. Therefo I think that the syntax in itself is correct.
According to the standard, a query that includes a grouping expressing
(i.e. GROUP BY) can retrieve columns listed in the grouping expression
(i.e. CATEGORY) and aggregates of other columns. This makes a certain
amount of sense if you think about a query like this:
select state, city from states group by state;
There are lots of cities in most states. Which is the right one to
return?
Now you know that for each CATEGORY value from ADDRESSES there is exactly
one CATEGORIES record, so, to you, your query makes sense. The SQL
committee doesn't trust you to make that sort of decision, so it requires
you to express your query like this:
>SELECT A.CATEGORY, COUNT(A.KEYINDEX), C.NAME^^^^^^^^
> FROM ADDRESS A, CATEGORIES C
> WHERE A.CATEGORY = C.C_NUMBER
> GROUP BY A.CATEGORY, C.NAME;
or this:
>SELECT A.CATEGORY, COUNT(A.KEYINDEX), MAX (C.NAME)(Actually the standard doesn't care about the paragraphing, the
> FROM ADDRESS A, CATEGORIES C ^^^^
> WHERE A.CATEGORY = C.C_NUMBER
> GROUP BY A.CATEGORY;
spaces, or the aliasing, those are all my prejudices.)
Regards,
Ann
www.ibphoenix.com
We have answers.