
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 0.80 KB | hits: 9 | expires: Never
SELECT *
FROM
(SELECT
bc.user_id,
u.login_name,
bo.total_balance,
'charge' as charge_or_payment,
type,
sum(amount)
FROM bill_charges bc JOIN
users u USING (user_id) JOIN
v_bill_overviews bo using (user_id)
WHERE bc.creation_tsz >= '2010.04.01' and bc.creation_tsz < '2010.05.01'
GROUP BY 1, 2, 3, 4,5
UNION
SELECT
bp.user_id,
u.login_name,
bo.total_balance,
'payment' as charge_or_payment,
type,
sum(amount)*-1
FROM
bill_payments bp JOIN
users u using (user_id) JOIN
v_bill_overviews bo using (user_id)
WHERE
bp.creation_tsz >= '2010.06.01' AND bp.creation_tsz < '2010.07.01' AND
type LIKE '%_refund'
GROUP BY 1, 2, 3, 4, 5
) AS biff
order by user_id;