Re: "select c1, method(c2) group by c1" returns all values of c2 for c1

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: Rolando Edwards <redwards(at)logicworks(dot)net>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: "select c1, method(c2) group by c1" returns all values of c2 for c1
Date: 2011-02-08 20:14:33
Message-ID: AANLkTik2oP0OjD2bYk-=ck0d7Np+stTO=-h2cdhDY+Wc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

2011/2/8 Emi Lu <emilu(at)encs(dot)concordia(dot)ca>:
> On 02/08/2011 02:51 PM, Rolando Edwards wrote:
>>
>> SELECT distinct c1,array_to_string(array(SELECT c2 FROM T1 B where
>> A.c1=B.c1),', ') from T1 A order by c1;
>>
>> Give it a Try !!!
>
>
> Thanks a lot! Very helpful!
>
> array_to_string() + array() is exactly what I am looking for!
>
> I just wonder that array_to_string() + array() will provide me good
> performance, right? If the calculation will be based on millions records.

it depend on number of groups. This is correlated subquery - it must
not be a best.

Regards

Pavel Stehule

the best speed gives a string_agg, but it is only in 9.0

>
> Thanks again!
> --
> Lu Ying
>
>
>
>
>
>> Rolando A. Edwards
>> MySQL DBA (SCMDBA)
>>
>> 155 Avenue of the Americas, Fifth Floor
>> New York, NY 10013
>> 212-625-5307 (Work)
>> 201-660-3221 (Cell)
>> AIM&  Skype : RolandoLogicWorx
>> redwards(at)logicworks(dot)net
>> http://www.linkedin.com/in/rolandoedwards
>>
>>
>> -----Original Message-----
>> From: pgsql-sql-owner(at)postgresql(dot)org
>> [mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Emi Lu
>> Sent: Tuesday, February 08, 2011 2:36 PM
>> To: pgsql-sql(at)postgresql(dot)org
>> Subject: [SQL] "select c1, method(c2) group by c1" returns all values of
>> c2 for c1
>>
>> Good afternoon,
>>
>> Is there a method to retrieve the following results:
>>
>> T1(c1 int, c2 varchar(128) )
>> ---------------------------------
>>
>>
>> (1, val1);
>> (1, val2);
>> (1, val3);
>> (2, val1);
>> (3, val5);
>> (3, val6);
>>
>> select c1, method(c2)
>> group by c1
>>
>> returns:
>> ============
>> 1,   "val1, val2, val3"
>> 2,   "val1"
>> 3,   "val5, val6"
>>
>>
>> Thanks a lot!
>>
>> --
>> Lu Ying
>>
>
>
> --
> Emi Lu, ENCS, Concordia University, Montreal H3G 1M8
> emilu(at)encs(dot)concordia(dot)ca        +1 514 848-2424 x5884
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Josh 2011-02-10 17:57:23 DELETE FROM takes forever
Previous Message Emi Lu 2011-02-08 20:09:29 Re: "select c1, method(c2) group by c1" returns all values of c2 for c1