views:

205

answers:

2

I'm trying to select the order total sum ($) and invoice count over a 5 day period in a single query. I can't seem to get this to happen though. The current query I have is here...

SELECT
    COUNT(id) as invoice_count,
    SUM(orderTotal) as orders_sum,
    UNIX_TIMESTAMP(created) as created
FROM ids_invoice
WHERE DATE_ADD(created, INTERVAL +1 DAY)
AND userId = 23 LIMIT 5'

I'm not entirely sure DATE_ADD is the right function I'm looking for.

Currently I'm getting....

Array ( 
    [0] => Array ( 
        [invoice_count] => 420
        [orders_total] => 97902.90
        [created] => 1252596560
    )
)

Array ( 
    [0] => Array ( 
        [invoice_count] => 68
        [orders_total] => 14193.20
        [created] => 1262900809
    )
)

I'd like to get something more like...

Array ( 
    [0] => Array ( 
        [invoice_count] => 18
        [orders_total] => 4902.90
        [date] => 04-19-2010
    )
)

Array ( 
    [0] => Array ( 
        [invoice_count] => 12
        [orders_total] => 5193.20
        [date] => 04-20-2010
    )
)

I'm fairly new to mysql date functions so perhaps I just missed the function I needed when going through mysql docs.

UPDATE I've updated my query... This still does not pull a row for each day that there were invoices for. It's only pulling invoices from the 19th when there are invoices from the 20th that meet the userId criteria.

SELECT
    COUNT(id) as invoice_count,
    SUM(orderTotal) as orders_sum,
    UNIX_TIMESTAMP(created) as created
FROM ids_invoice
WHERE
    created BETWEEN "2010-04-19 00:00:00" AND DATE_ADD("2010-04-19 00:00:00", INTERVAL +5 DAY) AND
    userId = 17
A: 
WHERE created <= NOW() AND created >=NOW() - INTERVAL 5 DAY

or it would be better to compare just DATE part of datetime:

WHERE DATE(created) <= date(NOW()) AND 
  DATE(created) >= DATE(NOW() - INTERVAL 5 DAY)
a1ex07
+1  A: 

To get records between a date span, use:

WHERE created BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY)

This example will get you records (assuming any exist for today, including time) for between today and days into the future. Look at DATE_SUB if you want to go into the past.

OMG Ponies
Is it possible for me to get default values for any figures that don't exist within the specified timeframe?
Webnet
@Webnet: You need a list of dates spanning the range you want, to LEFT JOIN onto the `IDS_INVOICE` table based on the date and the `created` value. MySQL doesn't have recursive subquerying - you can create a numbers table, or use a CROSS JOIN setup to get the date calculation you need.
OMG Ponies
I understand the logic of what you're saying, it makes perfect sense. You're saying I need to `SELECT COUNT(id) as invoice_count FROM ids_invoice LEFT JOIN ids_invoice WHERE created BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY)`I'm sure it's obvious.... but I'm not sure the syntax of what you're talking about.
Webnet
Having slept on it, I think I have a better understanding of what you're talking about and realize how wrong the query above is... I spent some more time on Google and failed to find an example of what you're talking about...
Webnet