Welcome Guest, Not a member yet? Register   Sign In
MYSQL Join Union Group By - two tables advice
#7

[eluser]danmontgomery[/eluser]
Code:
SELECT IFNULL( t_payments.total, 0 ) AS payments, IFNULL( t_expenses.total, 0 ) AS expenses, t_payments.month
FROM (
  SELECT SUM( payments.amount ) AS total, month( payments.pdate ) AS `month` FROM payments
  GROUP BY month( payments.pdate )
) AS t_payments
LEFT JOIN (
  SELECT SUM( expenses.gross ) AS total, month( expenses.expdate ) AS `month` FROM expenses
  GROUP BY month( expenses.expdate )
) AS t_expenses ON t_expenses.month = t_payments.month

That's gonna depend on their being payments in every month, I guess


Messages In This Thread
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 12:52 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 01:05 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 01:21 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 01:45 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 02:25 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 02:35 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 03:08 PM
MYSQL Join Union Group By - two tables advice - by El Forum - 02-17-2010, 03:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB