Skip to content

Instantly share code, notes, and snippets.

@troyk
Created March 7, 2013 13:19
Show Gist options
  • Save troyk/5108009 to your computer and use it in GitHub Desktop.
Save troyk/5108009 to your computer and use it in GitHub Desktop.
PostgreSQL, don't leave home without it... MongoDB <giggles>, I guess all the smart kids would know to prejoin their data... MySQL <giggles>, at least it could do half of it...
COPY(
SELECT
p.id as "id",
p.payor_email as "email",
to_char(p.created_at,'YYYYMMDD') as "date",
pgroup.gross*0.01::money as "gross",
pgroup.refunded*0.01::money as "refunded",
f.name as "fund",
pgroup.fee*0.01::money as "fee",
(SELECT 'Paid') as "status",
p.last4 as "last4",
p.payor_name as "name",
p.fingerprint as "card_fingerprint",
pgroup.transfer_ids as "transfer_ids"
FROM
(SELECT
pd.payment_id,
pd.fund_id,
sum(CASE WHEN pd.amount > 0 THEN pd.amount ELSE 0 END) as "gross",
sum(CASE WHEN pd.amount < 0 THEN pd.amount ELSE 0 END) as "refunded",
sum(pd.fee) as "fee",
array_agg(pd.payment_transfer_id) as "transfer_ids"
FROM payment_details pd
GROUP BY pd.payment_id, pd.fund_id) as pgroup
JOIN payments p ON p.id = pgroup.payment_id
JOIN funds f ON pgroup.fund_id = f.id
ORDER BY p.created_at
) to '/Users/troy/Projects/church/bayside_payments.csv' with csv header;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment