pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ragnar <gnari@hive.is>
To: Andreas Joseph Krogh <andreak@officenet.no>
Cc: pgsql-sql@postgresql.org
Subject: Re: Returning array of IDs as a sub-query with group-by
Date: Sat, 25 Aug 2007 21:02:19 +0000
Message-ID: <1188075739.12268.50.camel@localhost.localdomain> (raw)
In-Reply-To: <200708251755.30291.andreak@officenet.no>
References: <200708251710.57557.andreak@officenet.no>
	<200708251755.30291.andreak@officenet.no>

On lau, 2007-08-25 at 17:55 +0200, Andreas Joseph Krogh wrote:
> On Saturday 25 August 2007 17:10:57 Andreas Joseph Krogh wrote:

[snip]

> >  count | item_id | price | item_id_array
> > -------+---------+-------+---------------
> >      3 |       1 |   100 | {1,2,3}
> >      6 |       1 |   200 | {4,5,6,7,8,9}
> >      2 |       2 |   200 | {10,11}
> >
> > I tried this query which complains about an ungruoped column:
> >
> > SELECT COUNT(il.price), i.id AS item_id, il.price,
> >  ARRAY(SELECT a.id FROM item_log a WHERE a.id = il.id) AS item_id_array
> >   FROM item i, item_log il WHERE i.id = il.item_id GROUP BY il.price, i.id;
> >
> > ERROR:  subquery uses ungrouped column "il.id" from outer query
> >
> > Any hints?
> 
> I found the following CREATE AGGREGATE suggestion in the PG-docs:

[aggregate solution snipped]

> If someone knows of a way without introducing a new AGGREGATE I'm still 
> interrested.

you can allways do the ARRAY(SELECT...) outside the grouping:
# select *,(select ARRAY(
                         SELECT a.id 
                         FROM item_log as a 
                         WHERE foo.item_id=a.item_id
                               AND foo.price=a.price
                         )
           ) AS item_id_array 
from (
       select count(*),item_id, price 
       from item_log 
       group by item_id, price
     ) as foo;

 count | item_id | price | item_id_array 
-------+---------+-------+---------------
     3 |       1 |   100 | {1,2,3}
     6 |       1 |   200 | {4,5,6,7,8,9}
     2 |       2 |   200 | {10,11}
(3 rows)


but i suspect the aggregate will perform better

gnari





view thread (4+ messages)  latest in thread

Message-ID: <1188075739.12268.50.camel@localhost.localdomain>
Permalink:  ../1188075739.12268.50.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1188075739.12268.50.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gnari@hive.is, andreak@officenet.no
  Subject: Re: Returning array of IDs as a sub-query with group-by
  In-Reply-To: <1188075739.12268.50.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox