Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Created February 28, 2012 20:11
Show Gist options
  • Save ryandotsmith/1934824 to your computer and use it in GitHub Desktop.
Save ryandotsmith/1934824 to your computer and use it in GitHub Desktop.
select array aggregates
-- 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;
@ryandotsmith
Copy link
Author

@ryandotsmith
Copy link
Author

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