From walter.php@gmail.com Fri Sep 1 14:17:03 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 128899FB4B2 for ; Fri, 1 Sep 2006 11:17:03 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 90383-08-3 for ; Fri, 1 Sep 2006 11:11:31 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.203]) by postgresql.org (Postfix) with ESMTP id BF97D9FB512 for ; Fri, 1 Sep 2006 10:31:50 -0300 (ADT) Received: by nz-out-0102.google.com with SMTP id 14so668382nzn for ; Fri, 01 Sep 2006 06:31:49 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=EEffZTzMvhzO9bWEaYqf7q0pTEfQNSG8zrzEX6r4k5WMzEd+8uwzwSQeC4extM1epUIeB/Qwu0J1dlwvDhpzREJ58k9rAu/tWNK8eGEGMO/pYHFhC//Y5J2vqxK7c/MBTg1lFVvtoEBoWf+FyZpkp4Cxv1vkEnndgszMEbdNt1I= Received: by 10.65.211.13 with SMTP id n13mr2646595qbq; Fri, 01 Sep 2006 06:31:48 -0700 (PDT) Received: by 10.65.61.16 with HTTP; Fri, 1 Sep 2006 06:31:48 -0700 (PDT) Message-ID: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> Date: Fri, 1 Sep 2006 10:31:48 -0300 From: "Walter Cruz" To: pgsql-sql@postgresql.org Subject: help with pagila MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_34199_10952426.1157117508250" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.424 tagged_above=0 required=5 tests=AWL, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/1 X-Sequence-Number: 26212 ------=_Part_34199_10952426.1157117508250 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all. I'm with a little doubt. I'm testing the pagila (the postgres port of mysql sakila sample). Well, I was trying to translate the query: select film.film_id AS FID, film.title AS title, film.description AS description, category.name AS category, film.rental_rate AS price, film.length AS length, film.rating AS rating, group_concat(concat(actor.first_name,_utf8' ',actor.last_name) separator ',') AS actors from category inner join film on(category.category_id = film.category_id) inner join film_actor on(film.film_id = film_actor.film_id) inner join actor on(film_actor.actor_id = actor.actor_id) group by film.film_id; That cant be find here: http://www.stardata.it/articoli_en/mysql_sample_db_articoli_en.html I read a comment by David Fetter on a blog about group concat. I'm following his example: select film.title AS title, array_to_string(array_accum(actor.first_name || ' ' || actor.last_name),',') AS actors from film inner join film_actor on film.film_id = film_actor.film_id inner join actor on film_actor.actor_id = actor.actor_id GROUP BY film.title But, when I add another column on select, like, film_description, I get the following error: "ERROR: column "film.description" must appear in the GROUP BY clause or be used in an aggregate function" If I put that column on GROUP BY everything works ok. But I want understant why do I need to do that. Can someone teach me, please? []'s - Walter ------=_Part_34199_10952426.1157117508250 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all. I'm with a little doubt.

I'm testing the pagila (the postgres port of mysql sakila sample).

Well, I was trying to translate the query:

select
film.film_id AS FID,
film.title AS title,
film.description AS description,
category.name AS category,
film.rental_rate AS price,
film.length AS length,
film.rating AS rating,
group_concat(concat(actor.first_name,_utf8' ',actor.last_name) separator ',') AS actors
from
category
inner join film on(category.category_id = film.category_id)
inner join film_actor on(film.film_id = film_actor.film_id)
inner join actor on(film_actor.actor_id = actor.actor_id)
group by
film.film_id;

That cant be find here: http://www.stardata.it/articoli_en/mysql_sample_db_articoli_en.html

I read a comment by David Fetter on a blog about group concat. I'm following his example:

select
     film.title AS title, 
     array_to_string(array_accum( actor.first_name || ' ' || actor.last_name),',') AS actors
from
     film
     inner join film_actor on film.film_id = film_actor.film_id
     inner join actor on film_actor.actor_id = actor.actor_id
    GROUP BY film.title
    


But, when I add another column on select, like, film_description, I get the following error:

"ERROR:  column "film.description" must appear in the GROUP BY clause or be used in an aggregate function"

If I put that column on GROUP BY everything works ok. But I want understant why do I need to do that. Can someone teach me, please?

[]'s
- Walter
------=_Part_34199_10952426.1157117508250-- From ajs@crankycanuck.ca Fri Sep 1 17:14:32 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 5D6AB9FB478 for ; Fri, 1 Sep 2006 14:14:32 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 09029-02-9 for ; Fri, 1 Sep 2006 14:13:53 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from main2.mycybernet.net (main2.mycybernet.net [209.222.63.140]) by postgresql.org (Postfix) with ESMTP id 5F0329FB8EC for ; Fri, 1 Sep 2006 12:21:22 -0300 (ADT) Received: from 227-54-222-209.mycybernet.net ([209.222.54.227] helo=phlogiston.dydns.org) by main2.mycybernet.net with esmtp (Exim 4.62) (envelope-from ) id 1GJB3q-0004lh-Gb for pgsql-sql@postgresql.org; Fri, 01 Sep 2006 11:36:02 -0400 Received: by phlogiston.dydns.org (Postfix, from userid 1000) id C78864051; Fri, 1 Sep 2006 11:21:16 -0400 (EDT) Date: Fri, 1 Sep 2006 11:21:16 -0400 From: Andrew Sullivan To: pgsql-sql@postgresql.org Subject: Re: help with pagila Message-ID: <20060901152116.GE17178@phlogiston.dyndns.org> References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> User-Agent: Mutt/1.5.9i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.219 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200609/2 X-Sequence-Number: 26213 On Fri, Sep 01, 2006 at 10:31:48AM -0300, Walter Cruz wrote: > "ERROR: column "film.description" must appear in the GROUP BY clause or be > used in an aggregate function" > > If I put that column on GROUP BY everything works ok. But I want understant > why do I need to do that. Can someone teach me, please? You need to because everything else is being grouped or aggregated. Otherwise, you should get one row for every match of film.description, and that's not what you want. (More precisely and yet still completely imprecise, that's not even something you can have, because of the way sets work.) A -- Andrew Sullivan | ajs@crankycanuck.ca In the future this spectacle of the middle classes shocking the avant- garde will probably become the textbook definition of Postmodernism. --Brad Holland From walter.php@gmail.com Fri Sep 1 19:52:12 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A4C969FB52F for ; Fri, 1 Sep 2006 16:52:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 24555-02-2 for ; Fri, 1 Sep 2006 16:51:56 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.192]) by postgresql.org (Postfix) with ESMTP id 77BB19FBA2D for ; Fri, 1 Sep 2006 14:26:40 -0300 (ADT) Received: by nz-out-0102.google.com with SMTP id 14so715325nzn for ; Fri, 01 Sep 2006 10:26:40 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=apyIfOLDW4jOJm5tQtJ22VapKqHoiXO7VfJjUPliwRjFQOuhEKToYW4jG7M5nUTFe7SaRsDVoA/9dV2qt2zslp4K3ty4iMFkSCYkDZIkLGymkMdIKFSf3qdP06c2T6mXWWifWuMc6QMVQCvAa7VXjIAb9Cg+QJJkgUHNTQR38Ek= Received: by 10.65.121.9 with SMTP id y9mr3089102qbm; Fri, 01 Sep 2006 10:26:40 -0700 (PDT) Received: by 10.65.61.16 with HTTP; Fri, 1 Sep 2006 10:26:39 -0700 (PDT) Message-ID: <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> Date: Fri, 1 Sep 2006 14:26:39 -0300 From: "Walter Cruz" To: "Andrew Sullivan" Subject: Re: help with pagila Cc: pgsql-sql@postgresql.org In-Reply-To: <20060901152116.GE17178@phlogiston.dyndns.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_36395_23680054.1157131599924" References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.59 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_30_40, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/3 X-Sequence-Number: 26214 ------=_Part_36395_23680054.1157131599924 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline So I can assume that the MySQL implementation is strange? (It accepts that kind of query) []'s - Walter On 9/1/06, Andrew Sullivan wrote: > > On Fri, Sep 01, 2006 at 10:31:48AM -0300, Walter Cruz wrote: > > "ERROR: column "film.description" must appear in the GROUP BY clause or > be > > used in an aggregate function" > > > > If I put that column on GROUP BY everything works ok. But I want > understant > > why do I need to do that. Can someone teach me, please? > > You need to because everything else is being grouped or aggregated. > Otherwise, you should get one row for every match of > film.description, and that's not what you want. (More precisely and > yet still completely imprecise, that's not even something you can > have, because of the way sets work.) > > A > > -- > Andrew Sullivan | ajs@crankycanuck.ca > In the future this spectacle of the middle classes shocking the avant- > garde will probably become the textbook definition of Postmodernism. > --Brad Holland > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > ------=_Part_36395_23680054.1157131599924 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline So I can assume that the MySQL implementation is strange? (It accepts that kind of query)

[]'s
- Walter

On 9/1/06, Andrew Sullivan < ajs@crankycanuck.ca> wrote:
On Fri, Sep 01, 2006 at 10:31:48AM -0300, Walter Cruz wrote:
> "ERROR:  column "film.description" must appear in the GROUP BY clause or be
> used in an aggregate function"
>
> If I put that column on GROUP BY everything works ok. But I want understant
> why do I need to do that. Can someone teach me, please?

You need to because everything else is being grouped or aggregated.
Otherwise, you should get one row for every match of
film.description, and that's not what you want.  (More precisely and
yet still completely imprecise, that's not even something you can
have, because of the way sets work.)

A

--
Andrew Sullivan  | ajs@crankycanuck.ca
In the future this spectacle of the middle classes shocking the avant-
garde will probably become the textbook definition of Postmodernism.
                --Brad Holland

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

------=_Part_36395_23680054.1157131599924-- From ajs@crankycanuck.ca Fri Sep 1 20:06:24 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 732859FB77C for ; Fri, 1 Sep 2006 17:06:24 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 25123-03-5 for ; Fri, 1 Sep 2006 17:05:39 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from main2.mycybernet.net (main2.mycybernet.net [209.222.63.140]) by postgresql.org (Postfix) with ESMTP id 3D4589FBA9F for ; Fri, 1 Sep 2006 14:40:51 -0300 (ADT) Received: from 227-54-222-209.mycybernet.net ([209.222.54.227] helo=phlogiston.dydns.org) by main2.mycybernet.net with esmtp (Exim 4.62) (envelope-from ) id 1GJDEl-000765-2G; Fri, 01 Sep 2006 13:55:27 -0400 Received: by phlogiston.dydns.org (Postfix, from userid 1000) id 9D3244051; Fri, 1 Sep 2006 13:40:40 -0400 (EDT) Date: Fri, 1 Sep 2006 13:40:40 -0400 From: Andrew Sullivan To: Walter Cruz Cc: Andrew Sullivan , pgsql-sql@postgresql.org Subject: Re: help with pagila Message-ID: <20060901174040.GF17178@phlogiston.dyndns.org> References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> User-Agent: Mutt/1.5.9i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.218 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200609/4 X-Sequence-Number: 26215 On Fri, Sep 01, 2006 at 02:26:39PM -0300, Walter Cruz wrote: > So I can assume that the MySQL implementation is strange? (It accepts that > kind of query) In my experience, it is almost never safe to assume that the MySQL approach to SQL bears anything but a passing resemblance to SQL proper. This is considerably better under recent releases, however, and I think you'd find, if you used the strict mode in the most recent release, that MySQL would choke on a query like you posted as well. A -- Andrew Sullivan | ajs@crankycanuck.ca The fact that technology doesn't work is no bar to success in the marketplace. --Philip Greenspun From tv@fuzzy.cz Fri Sep 1 20:37:47 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B34859FB4A2 for ; Fri, 1 Sep 2006 17:37:47 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 28382-02-3 for ; Fri, 1 Sep 2006 17:37:17 -0300 (ADT) X-Greylist: delayed 00:06:40.78576 by SQLgrey- Received: from zeus.gigaweb.cz (zeus.gigaweb.cz [81.0.236.19]) by postgresql.org (Postfix) with SMTP id 7CADC9FB54D for ; Fri, 1 Sep 2006 15:12:49 -0300 (ADT) Received: (qmail 31690 invoked from network); 1 Sep 2006 18:06:08 -0000 Received: from unknown (HELO ?192.168.1.101?) (89.102.195.231) by 0 with SMTP; 1 Sep 2006 18:06:08 -0000 Message-ID: <44F87689.5010403@fuzzy.cz> Date: Fri, 01 Sep 2006 20:06:01 +0200 From: Tomas Vondra User-Agent: Mail/News 1.5 (X11/20060424) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: help with pagila References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> In-Reply-To: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/5 X-Sequence-Number: 26216 > But, when I add another column on select, like, film_description, I get > the following error: > > "ERROR: column "film.description" must appear in the GROUP BY clause or > be used in an aggregate function" > > If I put that column on GROUP BY everything works ok. But I want > understant why do I need to do that. Can someone teach me, please? The reason is pretty simple - GROUPing actually means "sorting into boxes by values in the columns after the GROUP BY keyword" (and then applying some aggregate functions to these boxes, as for example COUNT, AVG etc.) Besides these aggregates, you can select a column that 'constant' for each of the boxes, that is all the rows in that box have the same value in this column. That's the case of the first SQL query you've posted - you're grouping by 'film_id', thus all the rows in a box have the same value in this column. And thanks to this you can select the value in the SELECT. But in the second query, you'd like to select another column (directly, not through an aggregate function) - title. But there could be different values for each row in the box (PostgreSQL doesn't know that the ID uniquely identifies the title). For example imagine you would group by 'published_year' instead of the 'film_id' - in this case there would be many different movies in the same box, in which case it's impossible to select 'title' for all of them. There are two ways to solve this: 1) add the 'title' to the GROUP BY clause, thus all the rows in a box have the same value of 'title' (and postgresql knows about that) 2) use a subselect film_id, (SELECT title FROM film WHERE film_id = film_outer.film_id) AS title FROM film AS film_outer ... GROUP BY film_id; Tomas From smarlowe@g2switchworks.com Fri Sep 1 21:43:03 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id E765A9FB69B for ; Fri, 1 Sep 2006 18:43:03 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 02444-05-6 for ; Fri, 1 Sep 2006 21:42:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from exchange.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by postgresql.org (Postfix) with ESMTP id 4EE329FB74C for ; Fri, 1 Sep 2006 16:58:59 -0300 (ADT) Received: from 10.10.1.37 ([10.10.1.37]) by exchange.g2switchworks.com ([10.10.1.2]) with Microsoft Exchange Server HTTP-DAV ; Fri, 1 Sep 2006 19:58:58 +0000 Received: from state.g2switchworks.com by mail.g2switchworks.com; 01 Sep 2006 14:58:58 -0500 Subject: Re: help with pagila From: Scott Marlowe To: Walter Cruz Cc: Andrew Sullivan , pgsql-sql@postgresql.org In-Reply-To: <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1157140737.4786.31.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Fri, 01 Sep 2006 14:58:58 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=SPF_PASS, UNPARSEABLE_RELAY X-Spam-Level: X-Archive-Number: 200609/6 X-Sequence-Number: 26217 On Fri, 2006-09-01 at 12:26, Walter Cruz wrote: > So I can assume that the MySQL implementation is strange? (It accepts > that kind of query) Yes, according to the SQL spec, you should generally get an error when you run a query like this: select field1, field2 from table group by field1 since you could theoretically get a different value for field2 each time you run the query. If the data looked like this: field1 | field2 -------+-------- 1 | 1 1 | 3 2 | 5 2 | 3 The possible answers to that query would be (1,1)(2,5), (1,1)(2,3), (1,3)(2,5), (1,3)(2,3) From tgl@sss.pgh.pa.us Fri Sep 1 21:55:25 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B4D559FB724 for ; Fri, 1 Sep 2006 18:55:25 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 40343-01 for ; Fri, 1 Sep 2006 18:54:55 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 8AC1B9FB7E8 for ; Fri, 1 Sep 2006 17:21:56 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k81KLmWo020521; Fri, 1 Sep 2006 16:21:48 -0400 (EDT) To: Andrew Sullivan cc: Walter Cruz , pgsql-sql@postgresql.org Subject: Re: help with pagila In-reply-to: <20060901174040.GF17178@phlogiston.dyndns.org> References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> <20060901174040.GF17178@phlogiston.dyndns.org> Comments: In-reply-to Andrew Sullivan message dated "Fri, 01 Sep 2006 13:40:40 -0400" Date: Fri, 01 Sep 2006 16:21:48 -0400 Message-ID: <20520.1157142108@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.125 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/7 X-Sequence-Number: 26218 Andrew Sullivan writes: > On Fri, Sep 01, 2006 at 02:26:39PM -0300, Walter Cruz wrote: >> So I can assume that the MySQL implementation is strange? (It accepts that >> kind of query) > In my experience, it is almost never safe to assume that the MySQL > approach to SQL bears anything but a passing resemblance to SQL > proper. This is considerably better under recent releases, however, > and I think you'd find, if you used the strict mode in the most > recent release, that MySQL would choke on a query like you posted as > well. If film_id is a primary key for film, then it's actually legal per SQL99 (though not in earlier SQL specs) to just GROUP BY film_id and then reference the other columns of film without explicit grouping, because clearly there can be only one value of them per film_id value. However the quoted query includes ungrouped references to other tables as well, and it's not immediately obvious that those references must have unique values for any one value of film_id. It's possible that MySQL is taking the trouble to validate that this query is legal per SQL99 rules, but I'd find it quite surprising given their project philosophy --- fine points like whether a query has a single right answer tend not to matter to them. Postgres currently implements only the older SQL92 rules, under which you gotta explicitly GROUP BY all the columns you want to reference outside aggregate functions. We'll probably implement some parts of the looser SQL99 rules in the future, but that's where it stands today. regards, tom lane From tv@fuzzy.cz Fri Sep 1 21:57:15 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9CF709FB53B for ; Fri, 1 Sep 2006 18:57:15 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 39316-01 for ; Fri, 1 Sep 2006 18:56:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from zeus.gigaweb.cz (zeus.gigaweb.cz [81.0.236.19]) by postgresql.org (Postfix) with SMTP id 595329FB5CD for ; Fri, 1 Sep 2006 17:25:49 -0300 (ADT) Received: (qmail 28694 invoked from network); 1 Sep 2006 20:25:49 -0000 Received: from unknown (HELO ?192.168.1.101?) (89.102.195.231) by 0 with SMTP; 1 Sep 2006 20:25:49 -0000 Message-ID: <44F89746.2010903@fuzzy.cz> Date: Fri, 01 Sep 2006 22:25:42 +0200 From: Tomas Vondra User-Agent: Mail/News 1.5 (X11/20060424) MIME-Version: 1.0 CC: pgsql-sql@postgresql.org Subject: Re: help with pagila References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> In-Reply-To: <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/8 X-Sequence-Number: 26219 > So I can assume that the MySQL implementation is strange? (It accepts > that kind of query) Yes, MySQL behaves strangely in this case (as well as in several other cases). I wouldn't rely on this as it probably can choose different values each time (although as far as I remember I haven't seen this). t.v. From walter.php@gmail.com Fri Sep 1 22:04:06 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9F1259FB3C0 for ; Fri, 1 Sep 2006 19:04:06 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 42361-01 for ; Fri, 1 Sep 2006 19:03:15 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from nz-out-0102.google.com (nz-out-0102.google.com [64.233.162.194]) by postgresql.org (Postfix) with ESMTP id 120629FB3BD for ; Fri, 1 Sep 2006 17:46:03 -0300 (ADT) Received: by nz-out-0102.google.com with SMTP id 14so747674nzn for ; Fri, 01 Sep 2006 13:46:02 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:references; b=LcN81dox6VZAXCln8qtx6lKB8lswpVWkDlzk2nibJxkUpB8inLWldx21tpMihqN6iEj+aRJiu97Io2AJrDTh2n3HfVxQJ4uA/4e6zBe1af7u7kId33dKzm4/C0+E6vKJkr9edy86cP4YH/5UZ+gBbzg7OGL4IcSVz4GYhzYP8Wc= Received: by 10.65.219.1 with SMTP id w1mr975937qbq; Fri, 01 Sep 2006 13:46:02 -0700 (PDT) Received: by 10.65.61.16 with HTTP; Fri, 1 Sep 2006 13:46:02 -0700 (PDT) Message-ID: <32cabba0609011346v5e2cde53w6d85ec78f0db3a24@mail.gmail.com> Date: Fri, 1 Sep 2006 17:46:02 -0300 From: "Walter Cruz" To: pgsql-sql@postgresql.org Subject: Re: help with pagila In-Reply-To: <20520.1157142108@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_37653_31413001.1157143562105" References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> <20060901174040.GF17178@phlogiston.dyndns.org> <20520.1157142108@sss.pgh.pa.us> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.388 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200609/9 X-Sequence-Number: 26220 ------=_Part_37653_31413001.1157143562105 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Thank you all. I found myself with the same trouble in last week, when I tried to port mambo CMS to PostgreSQL. After some work, In fall i a query like the one that was quoted by Andrew and decided to stop. In that time, my guess wae that something was wrong with MySQL. (I don't know.. I have seen queries like that on a lot of mysql free projects. Maybe we can put that on a FAQ?) []'s - Walter On 9/1/06, Tom Lane wrote: > > Andrew Sullivan writes: > > On Fri, Sep 01, 2006 at 02:26:39PM -0300, Walter Cruz wrote: > >> So I can assume that the MySQL implementation is strange? (It accepts > that > >> kind of query) > > > In my experience, it is almost never safe to assume that the MySQL > > approach to SQL bears anything but a passing resemblance to SQL > > proper. This is considerably better under recent releases, however, > > and I think you'd find, if you used the strict mode in the most > > recent release, that MySQL would choke on a query like you posted as > > well. > > If film_id is a primary key for film, then it's actually legal per SQL99 > (though not in earlier SQL specs) to just GROUP BY film_id and then > reference the other columns of film without explicit grouping, because > clearly there can be only one value of them per film_id value. However > the quoted query includes ungrouped references to other tables as well, > and it's not immediately obvious that those references must have unique > values for any one value of film_id. > > It's possible that MySQL is taking the trouble to validate that this > query is legal per SQL99 rules, but I'd find it quite surprising given > their project philosophy --- fine points like whether a query has a > single right answer tend not to matter to them. > > Postgres currently implements only the older SQL92 rules, under which > you gotta explicitly GROUP BY all the columns you want to reference > outside aggregate functions. We'll probably implement some parts of the > looser SQL99 rules in the future, but that's where it stands today. > > regards, tom lane > ------=_Part_37653_31413001.1157143562105 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Thank you all.

I found myself with the same trouble in last week, when I tried to port mambo CMS to PostgreSQL.

After some work, In fall i a query like the one that was quoted by Andrew and decided to stop.

In that time, my guess wae that something was wrong with MySQL.

(I don't know.. I have seen queries like that on a lot of mysql free projects. Maybe we can put that on a FAQ?)

[]'s
- Walter


On 9/1/06, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Andrew Sullivan <ajs@crankycanuck.ca> writes:
> On Fri, Sep 01, 2006 at 02:26:39PM -0300, Walter Cruz wrote:
>> So I can assume that the MySQL implementation is strange? (It accepts that
>> kind of query)

> In my experience, it is almost never safe to assume that the MySQL
> approach to SQL bears anything but a passing resemblance to SQL
> proper.  This is considerably better under recent releases, however,
> and I think you'd find, if you used the strict mode in the most
> recent release, that MySQL would choke on a query like you posted as
> well.

If film_id is a primary key for film, then it's actually legal per SQL99
(though not in earlier SQL specs) to just GROUP BY film_id and then
reference the other columns of film without explicit grouping, because
clearly there can be only one value of them per film_id value.  However
the quoted query includes ungrouped references to other tables as well,
and it's not immediately obvious that those references must have unique
values for any one value of film_id.

It's possible that MySQL is taking the trouble to validate that this
query is legal per SQL99 rules, but I'd find it quite surprising given
their project philosophy --- fine points like whether a query has a
single right answer tend not to matter to them.

Postgres currently implements only the older SQL92 rules, under which
you gotta explicitly GROUP BY all the columns you want to reference
outside aggregate functions.  We'll probably implement some parts of the
looser SQL99 rules in the future, but that's where it stands today.

                        regards, tom lane

------=_Part_37653_31413001.1157143562105-- From sszabo@megazone.bigpanda.com Fri Sep 1 22:24:12 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 801349FB56B for ; Fri, 1 Sep 2006 19:24:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 46914-04-4 for ; Fri, 1 Sep 2006 19:23:29 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 812F29FB610 for ; Fri, 1 Sep 2006 18:13:04 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 80A8135115; Fri, 1 Sep 2006 14:12:59 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 7EBFA35112; Fri, 1 Sep 2006 14:12:59 -0700 (PDT) Date: Fri, 1 Sep 2006 14:12:59 -0700 (PDT) From: Stephan Szabo To: Walter Cruz Cc: pgsql-sql@postgresql.org Subject: Re: help with pagila In-Reply-To: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> Message-ID: <20060901140554.N12215@megazone.bigpanda.com> References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.126 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200609/10 X-Sequence-Number: 26221 On Fri, 1 Sep 2006, Walter Cruz wrote: > Hi all. I'm with a little doubt. > > I'm testing the pagila (the postgres port of mysql sakila sample). > > Well, I was trying to translate the query: > > select > film.film_id AS FID, > film.title AS title, > film.description AS description, > category.name AS category, > film.rental_rate AS price, > film.length AS length, > film.rating AS rating, > group_concat(concat(actor.first_name,_utf8' ',actor.last_name) > separator ',') AS actors > from > category > inner join film on(category.category_id = film.category_id) > inner join film_actor on(film.film_id = film_actor.film_id) > inner join actor on(film_actor.actor_id = actor.actor_id) > group by > film.film_id; Assuming that film_id is the primary key on film and category_id is the primary key on category, I think you'd be allowed to have the other column references in SQL03 (and 99?) but not in SQL92 (which is the version that PostgreSQL currently implements). IIRC, the later specs allow you to not mention columns in group by that are functionally dependant on other columns that are mentioned. From schabi@logix-tt.com Mon Sep 4 08:10:16 2006 X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 7EBC79FB505 for ; Mon, 4 Sep 2006 05:10:16 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 50052-02-6 for ; Mon, 4 Sep 2006 05:10:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.logix-tt.com (mail.logix-tt.com [212.211.145.186]) by postgresql.org (Postfix) with ESMTP id 7C9F19FA5EA for ; Mon, 4 Sep 2006 04:55:59 -0300 (ADT) Received: from kingfisher.intern.logix-tt.com (80-219-241-140.dclient.hispeed.ch [80.219.241.140]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.logix-tt.com (Postfix) with ESMTP id CDCD8656BE for ; Mon, 4 Sep 2006 09:58:09 +0200 (CEST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by kingfisher.intern.logix-tt.com (Postfix) with ESMTP id B84EB18151EBC for ; Mon, 4 Sep 2006 09:55:56 +0200 (CEST) Message-ID: <44FBDC0C.5030902@logix-tt.com> Date: Mon, 04 Sep 2006 09:55:56 +0200 From: Markus Schaber Organization: Logical Tracking and Tracing International AG, Switzerland User-Agent: Thunderbird 1.5.0.5 (X11/20060812) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: help with pagila References: <32cabba0609010631t233d13f9o38edba0483a2446c@mail.gmail.com> <20060901152116.GE17178@phlogiston.dyndns.org> <32cabba0609011026r6616a73bhe80b336f18aa84ac@mail.gmail.com> <20060901174040.GF17178@phlogiston.dyndns.org> <20520.1157142108@sss.pgh.pa.us> In-Reply-To: <20520.1157142108@sss.pgh.pa.us> X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.155 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200609/16 X-Sequence-Number: 26227 Hi, Tom, Tom Lane wrote: > If film_id is a primary key for film, then it's actually legal per SQL99 > (though not in earlier SQL specs) to just GROUP BY film_id and then > reference the other columns of film without explicit grouping, because > clearly there can be only one value of them per film_id value. However > the quoted query includes ungrouped references to other tables as well, > and it's not immediately obvious that those references must have unique > values for any one value of film_id. In situations like this, I often missed (and sometimes implemented) a simple "first()" aggregate to put around those other columns. Some of those cases could be fixed by creative use of "DISTINCT ON", but sometimes lead to suboptimal query plans (due to the useless sorting). Markus -- Markus Schaber | Logical Tracking&Tracing International AG Dipl. Inf. | Software Development GIS Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org