I am having a struggle with displaying data properly on the page. I am running a query for a billing report that searches all outstanding bills and I want to display them on a single page with the outstanding balance being displayed by how old the balance is. So if client ABC has 10 invoices over the past year and some of them have outstanding balances, I would like to display those balances in columns of 0-30 / 30-60 / 60-90 / and 90+ days old. Here is what I have…
$sql17 = "SELECT $mysql_billing.primary_key, $mysql_billing.a_name, $mysql_billing.invoicedate, $mysql_billing.finaltotal, $mysql_billing_dates.paidtotal,
($mysql_billing.finaltotal - $mysql_billing_dates.paidtotal) AS total
FROM
(SELECT $mysql_billing.primary_key, $mysql_billing.a_name, $mysql_billing.login_username, $mysql_billing.invoicedate,
SUM($mysql_billing.custotal) AS finaltotal
FROM $mysql_billing where $today >= invoicedate AND $start_search_prev180_date <= invoicedate AND custotal != payments GROUP BY login_username) $mysql_billing
LEFT JOIN
(SELECT $mysql_billing_dates.id, $mysql_billing_dates.username,
SUM($mysql_billing_dates.amount) AS paidtotal
FROM $mysql_billing_dates where complete != 'yes' GROUP BY username) $mysql_billing_dates
ON ($mysql_billing.login_username = $mysql_billing_dates.username)
GROUP BY a_name ORDER BY a_name ASC";
this seems to print out the results but how do you get the results into different columns based off of more than 1 date variable? Do I have to do 4 separate queries?
Your SQL Fiddle was close, but a few of your joins were incorrect.
Here is the updated Fiddle.