Skip to content

Instantly share code, notes, and snippets.

@brandon-beacher
Created April 22, 2011 14:59
Show Gist options
  • Save brandon-beacher/936834 to your computer and use it in GitHub Desktop.
Save brandon-beacher/936834 to your computer and use it in GitHub Desktop.
select
'FundPledge' as pledge_type,
fund_pledges.id as pledge_id,
fund_pledges.created_at,
coalesce(sum(fund_collections.amount), 0) >= fund_pledges.amount as completed
from
fund_pledges
left join
fund_collections on fund_pledges.id = fund_collections.fund_pledge_id and fund_collections.paid = 1
group by
fund_pledges.id,
fund_pledges.created_at
union
select
'GoodPledge' as pledge_type,
good_pledges.id as pledge_id,
good_pledges.created_at,
coalesce(sum(good_collections.quantity), 0) >= good_pledges.quantity as completed
from
good_pledges
left join
good_collections on good_pledges.id = good_collections.good_pledge_id
group by
good_pledges.id,
good_pledges.created_at
union
select
'ServicePledge' as pledge_type,
service_pledges.id as pledge_id,
service_pledges.created_at,
coalesce(sum(service_collections.duration), 0) >= service_pledges.duration as completed
from
service_pledges
left join
service_collections on service_pledges.id = service_collections.service_pledge_id
group by
service_pledges.id,
service_pledges.created_at
order by completed, created_at desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment