Skip to content

Instantly share code, notes, and snippets.

@zhangxu
Last active August 29, 2015 14:05
Show Gist options
  • Save zhangxu/6f8123cacb7c5105f977 to your computer and use it in GitHub Desktop.
Save zhangxu/6f8123cacb7c5105f977 to your computer and use it in GitHub Desktop.
PostgreSQL `With` Clause
with parcel_ids as (select id from parcels where ranch_id = 57),
zone_ids as (select id from zones where parcel_id in (select id from parcel_ids))
select extract(month from date) as month, category, sub_category, count(*) from events where (area_type = 'parcel' and area_id in (select id from parcel_ids)) or (area_type = 'zone' and area_id in (select id from zone_ids))
group by extract(month from date), category, sub_category;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment