pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedarray_agg order by
2+ messages / 2 participants
[nested] [flat]
* array_agg order by
@ 2012-02-28 03:50 jdmorgan <jdmorgan@unca.edu>
0 siblings, 1 reply; 2+ messages in thread
From: jdmorgan @ 2012-02-28 03:50 UTC (permalink / raw)
To: pgsql-sql
Hello,
I am using a array_agg to get a subset of data to use in a
query.However, I can't figure out how to get the data returned in the
array_agg function to sort with an order by function.Any help would be
appreciated.Here is my query as it is now.I am using postgresql 8:
select pestproxshapes.pt_id, pestproxshapes.gid,
array_to_string(array_agg(coalesce(pestproxdata.agent || ' | Closest
Agent (km): ' || round(pestproxdata.metric1,2) || ' | Acres: ' ||
round(pestproxdata.metric2, 2) || ' | count: ' || pestproxdata.count ||
' | yr: ' || pestproxdata.year || ' | prob: ' ||
round(pestproxdata.prob,2) || ' | host: ' || pestproxdata.host, 'no
agents reported within 75km')), '\n') as AGENTS from pestproxshapes left
outer join pestproxdata on pestproxshapes.pt_id=pestproxdata.pt_id where
pestproxshapes.pt_id = 8089 group by pestproxshapes.pt_id,
pestproxshapes.gid;
Thanks,
Derek
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: array_agg order by
@ 2012-02-28 04:15 Tom Lane <tgl@sss.pgh.pa.us>
parent: jdmorgan <jdmorgan@unca.edu>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2012-02-28 04:15 UTC (permalink / raw)
To: jdmorgan <jdmorgan@unca.edu>; +Cc: pgsql-sql
jdmorgan <jdmorgan@unca.edu> writes:
> I am using a array_agg to get a subset of data to use in a
> query.However, I can't figure out how to get the data returned in the
> array_agg function to sort with an order by function.Any help would be
> appreciated.Here is my query as it is now.I am using postgresql 8:
Well, if you were using 9.0 or later, you could do this "right":
select array_agg(foo order by bar), otherstuff from ..tables..;
You can kind of get there in older versions with a sub-select:
select array_agg(foo), otherstuff
from
(select foo, otherstuff from ..tables.. order by whatever) ss;
However that's a bit shaky because you can't do very much in the outer
query, like say grouping, without risking messing up the sort ordering.
You have to keep a close eye on the EXPLAIN output for your query to
make sure nothing re-sorts the data before it gets to the Aggregate
step.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2012-02-28 04:15 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-02-28 03:50 array_agg order by jdmorgan <jdmorgan@unca.edu>
2012-02-28 04:15 ` Tom Lane <tgl@sss.pgh.pa.us>
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