MYSQL sum: Multiple sums with different criteria

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
FROM
cust,
cust_package,
cust_info,
package_type
WHERE
cust.cust_id=cust_info.cust_id AND
cust_info.cust_id=cust_package.cust_id AND
cust_package.pack_id=package_type.pack_id AND
cp_start_stamp > UNIX_TIMESTAMP((SELECT tdate FROM TempDel)) AND
cp_start_stamp < UNIX_TIMESTAMP(Date_add( (SELECT tdate FROM TempDel), INTERVAL 1 month));
the_cymanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you can, using the CASE statement:
SELECT SUM(CASE WHEN INVOICE_DATE >= DATE_SUB(NOW, INTERVAL 3 MONTH) THEN INVOICE_AMOUNT ELSE 0 END) REVENUE_LAST_3_MONTH
, SUM(CASE WHEN INVOICE_DATE >= DATE_SUB(NOW, INTERVAL 6 MONTH) THEN INVOICE_AMOUNT ELSE 0 END) REVENUE_LAST_6_MONTH
, SUM(CASE WHEN INVOICE_DATE >= DATE_SUB(NOW, INTERVAL 10 MONTH) THEN INVOICE_AMOUNT ELSE 0 END) REVENUE_LAST_10_MONTH
FROM yourtable
WHERE INVOICE_DATE >= DATE_SUB(NOW, INTERVAL 10 MONTH) 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.