Created
November 12, 2013 21:37
-
-
Save jpotts18/7439255 to your computer and use it in GitHub Desktop.
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
SELECT | |
users.display_name, | |
users.paypal_username, | |
users.billing_name, | |
users.address1, | |
users.address2, | |
users.city, | |
users.state, | |
users.email, | |
users.phone, | |
users.ssn_number, | |
COALESCE(win.winnings,0) winnings, | |
COALESCE(refund.refunds,0) refunds, | |
COALESCE(payment.payments,0) payments, | |
COALESCE((win.winnings - (payment.payments - refund.refunds)),0) net | |
FROM users | |
LEFT JOIN ( | |
SELECT transactions.to_key, SUM(transactions.amount) winnings | |
FROM transactions | |
WHERE transactions.type = 'Winning' | |
AND extract(year from created) = extract(year from NOW()) | |
GROUP BY transactions.to_key) win | |
ON users.id = win.to_key | |
LEFT JOIN ( | |
SELECT transactions.to_key, SUM(transactions.amount) refunds | |
FROM transactions | |
WHERE transactions.type = 'Return Fee Payment' | |
AND extract(year from created) = extract(year from NOW()) | |
GROUP BY transactions.to_key) refund | |
ON users.id = refund.to_key | |
LEFT JOIN ( | |
SELECT transactions.from_key, SUM(transactions.amount) payments | |
FROM transactions | |
WHERE transactions.type = 'Fee Payment' | |
AND extract(year from created) = extract(year from NOW()) | |
GROUP BY transactions.from_key) payment | |
ON users.id = payment.from_key | |
WHERE | |
COALESCE((win.winnings - (payment.payments - refund.refunds)),0) > 60›0 | |
; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment