Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 8828663387D for ; Tue, 15 Sep 2009 05:32:00 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 97366-07 for ; Tue, 15 Sep 2009 08:31:45 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id ECC85632BF9 for ; Tue, 15 Sep 2009 05:31:49 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.3/8.14.3) with ESMTP id n8F8VjxC061743 for ; Tue, 15 Sep 2009 05:31:46 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.3/8.14.3/Submit) id n8F8CviW055332 for pgsql-sql@postgresql.org; Tue, 15 Sep 2009 05:12:57 -0300 (ADT) (envelope-from news) From: wstrzalka X-Newsgroups: pgsql.sql Subject: Re: ordered by join? ranked aggregate? how to? Date: Tue, 15 Sep 2009 01:12:59 -0700 (PDT) Organization: http://groups.google.com Lines: 64 Message-ID: <8e3cb4f4-084d-4983-b83a-6efbf6b09308@33g2000vbe.googlegroups.com> References: <20090915075605.GB8046@a-kretschmer.de> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Complaints-To: groups-abuse@google.com Complaints-To: groups-abuse@google.com Injection-Info: 33g2000vbe.googlegroups.com; posting-host=89.174.79.62; posting-account=9WkiLgoAAAAu9mBUBORgx8ddEnvDVjcP User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14 (.NET CLR 3.5.30729), gzip(gfe), gzip(gfe) To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.299 tagged_above=-10 required=5 tests=AWL=1.300, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/68 X-Sequence-Number: 33133 On 15 Wrz, 10:10, wstrzalka wrote: > On 15 Wrz, 09:56, andreas.kretsch...@schollglas.com ("A. Kretschmer") > wrote: > > > > > In response to wstrzalka : > > > > What I need is to join 2 tables > > > > CREATE TABLE master( > > > =A0 =A0 id INT4 > > > ); > > > > CREATE TABLE slave ( > > > =A0 =A0 master_id INT4, > > > =A0 =A0 rank INT4, > > > =A0 =A0 value TEXT); > > > > What I need is to make the query: > > > > SELECT m.id, array_agg(s.value) AS my_problematic_array > > > FROM master AS m LEFT JOIN slave AS s ON (m.id =3D s.master_id) > > > GROUP BY m.id; > > > Faster solution (compared to my other email): > > > test=3D# select id, array_agg(value) from (SELECT m.id, s.value FROM > > master AS m LEFT JOIN slave AS s ON (m.id =3D s.master_id) order by 1,2= ) > > foo group by 1; > > =A0id | array_agg > > ----+----------- > > =A0 1 | {3,5,7} > > =A0 2 | {NULL} > > (2 rows) > > > Andreas > > -- > > Andreas Kretschmer > > Kontakt: =A0Heynitz: 035242/47150, =A0 D1: 0160/7141639 (mehr: -> Heade= r) > > > -- > > Sent via pgsql-sql mailing list (pgsql-...@postgresql.org) > > To make changes to your subscription:http://www.postgresql.org/mailpref= /pgsql-sql > > Yes. And No :) > > The problem is that in original query the aggregate can be used more > then once :/ > > So in fact it's like: > > SELECT m.id, array_agg(s1.value), array_agg(s2.value) > FROM master AS m LEFT JOIN slave AS s1 ON (m.id =3D s1.master_id AND > SOME_CONDITION_ON_S1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0LEFT JOIN = slave AS s2 ON (m.id =3D > s2.master_id AND SOME_OTHER_CONDITION_ON_S1) > GROUP BY m.id; However it still may work as the order by is the same :) Will try ...