Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Total Daily Sale for 7 days

Tags:

sql

mysql

I am trying to get the date and SUM(total) for last 7 days. I am trying this code and getting date NULL and daily_sale 0.00

SELECT
  date,
  IFNULL( SUM( total ), 0 ) AS daily_sale
FROM sales
WHERE date BETWEEN NOW()
    AND DATE_SUB(NOW(), INTERVAL 7 DAY)

I am note sure how to get the date and daily total sale. I want get results as

date       daily_sale    
2013-01-29 500.00
2013-01-28 500.00
2013-01-27 500.00
2013-01-26 500.00
...
2013-01-23 500.00
like image 893
Saleem Avatar asked Jan 21 '26 02:01

Saleem


1 Answers

You can try with interval - 7 day:

SELECT  date, SUM(Coalese(total,0)) AS daily_sale
FROM yourtable
WHERE date BETWEEN NOW()  AND DATE_ADD(NOW(), INTERVAL -7 DAY)

Not sure why between didn't work, check this out:

SQLFIDDLE DEMO

select dates, sum(coalesce(daily_sale,0)) as total
from sales
where dates <= NOW()
and dates >= Date_add(Now(),interval - 7 day)
group by dates
;

|                          DATES | TOTAL |
------------------------------------------
| January, 23 2013 00:00:00+0000 |   500 |
| January, 24 2013 00:00:00+0000 |   500 |
| January, 27 2013 00:00:00+0000 |  1500 |
| January, 28 2013 00:00:00+0000 |  1000 |
| January, 29 2013 00:00:00+0000 |   500 |
like image 94
bonCodigo Avatar answered Jan 23 '26 20:01

bonCodigo