Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.80 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. SELECT *
  2. FROM
  3.         (SELECT
  4.                 bc.user_id,
  5.                 u.login_name,
  6.                 bo.total_balance,
  7.                 'charge' as charge_or_payment,
  8.                 type,
  9.                 sum(amount)
  10.         FROM bill_charges bc JOIN
  11.         users u USING (user_id) JOIN
  12.         v_bill_overviews bo using (user_id)
  13.         WHERE bc.creation_tsz >= '2010.04.01' and bc.creation_tsz < '2010.05.01'
  14.         GROUP BY 1, 2, 3, 4,5
  15. UNION
  16.         SELECT
  17.                 bp.user_id,
  18.                 u.login_name,
  19.                 bo.total_balance,
  20.                 'payment' as charge_or_payment,
  21.                 type,
  22.                 sum(amount)*-1
  23.         FROM
  24.                  bill_payments bp JOIN
  25.                  users u using (user_id) JOIN
  26.                  v_bill_overviews bo using (user_id)
  27.         WHERE
  28.                 bp.creation_tsz >= '2010.06.01' AND bp.creation_tsz < '2010.07.01' AND
  29.                 type LIKE '%_refund'
  30.         GROUP BY 1, 2, 3, 4, 5
  31.         ) AS biff
  32. order by user_id;