MYSQL sum: Multiple sums with different criteria
Posted on 2008-11-09
I am using MySQL 4 (no VIEWS)
I have a long, complex query of invoices that returns a table of:
INVOICE DATE, INVOICE AMOUNT
Now, I want to create a few sums ALL IN ONE QUERY:
- A sum of the revenue of these invoices from the last 3 months.
- A sum of the revenue of these invoices for the last 6 months.
- A sum of the revenue of these invoices for the last 10 months.
I _could_ just copy and paste this rediculously long query, but I'm sure there's a better way.
Here's the Query that produces a table of invoices dates paid and the amount:
[Keep in mind that tdate just has '2008-03-01 12:00:00']
SELECT cust.invoice_date_paid, cust.invoice_amount_paid
cp_start_stamp > UNIX_TIMESTAMP((SELECT tdate FROM TempDel)) AND
cp_start_stamp < UNIX_TIMESTAMP(Date_add( (SELECT tdate FROM TempDel), INTERVAL 1 month));