Created
February 28, 2012 20:11
-
-
Save ryandotsmith/1934824 to your computer and use it in GitHub Desktop.
select array aggregates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This will give us a table that has a row for each resource but includes all of the events related to the | |
-- resource on the row. Calling array_agg will load all of the data in memory which means that I can do math | |
-- on the set really quickly. Also, callers of this query will be able to get all of the data for the invoice | |
-- in one shot. | |
create view invoice as | |
select | |
a.resource_id, | |
sum(a.qty) as qty, | |
array_agg(hstore(a.*)) as events_collection | |
from events | |
group by a.resource_id; |
Oh, and you can return an array of hstores :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.postgresql.org/docs/9.1/static/functions-aggregate.html