1

I have two tables, customers and sales. I want to count sales for each customer and create a table of sales per month for each store.

I would like to produce something like;

------------------------------
month  |  customers  | sales  |
------------------------------
1/2013 |      5      |   2    |
2/2013 |      21     |   9    |
3/2013 |      14     |   4    |
4/2013 |      9      |   3    |

but I am having trouble getting the sales count to be correct when using the following;

SELECT CONCAT(MONTH(c.added), '/', YEAR(c.added)), count(c.id), count(s.id)
FROM customers c
LEFT JOIN sales s 
ON s.customer_id = c.id AND MONTH(c.added) = MONTH(s.added) AND YEAR(c.added) = YEAR(s.added)
WHERE c.store_id = 1
GROUP BY YEAR(c.added), MONTH(c.added);

Customers table;

-------------------------------
id    |   store_id  | added    |
-------------------------------
1     |      1      |2013-02-01 |
2     |      1      |2013-02-02 |
3     |      1      |2013-03-16 |

sales table;

---------------------------------
id    |   added    | customer_id |
---------------------------------
1     | 2013-02-18 |     3       |
2     | 2013-03-02 |     2       |
3     | 2013-03-16 |     3       |

Can anyone help here?

thanks

Christy Herron
  • 694
  • 1
  • 7
  • 16

2 Answers2

1

(Updated) The existing query will only count sales made in the same month that the customer was added. Try this, instead:

SELECT CONCAT(MONTH(sq.added), '/', YEAR(sq.added)) month_year,
       sum(sq.customer_count), 
       sum(sq.sales_count)
FROM (select s.added, 0 customer_count, 1 sales_count
      from customers c
      JOIN sales s ON s.customer_id = c.id
      WHERE c.store_id = 1
      union all
      select added, 1 customer_count, 0 sales_count
      from customers
      WHERE store_id = 1) sq
GROUP BY YEAR(sq.added), MONTH(sq.added);
  • thanks @Mark, thats great. You seem to have found the issue with my own query. Unfortunately I cant guarantee that a sale will be made in the same month as the customer was added. Is it possible for you to create the 'more complicated' query? Thank you – Christy Herron Jun 04 '13 at 12:30
  • @ChristyHerron: This should return new customers per month, and sales per month, for the specified store - note that the sales_count is grouped on the sales table added date. What leads you to think it is returning too many results? Have you tried checking the results for a specific month against the actual records in the database? –  Jun 04 '13 at 15:38
  • Thanks @Mark, my mistake, your query works great. Thank you so much, im very grateful. – Christy Herron Jun 04 '13 at 17:25
  • @ChristyHerron: I'm glad I could help. –  Jun 04 '13 at 17:38
0
SELECT c.* , s.sales_count<br>
FROM customers c<br>
LEFT JOIN (SELECT customer_id, count(id) as sales_count FROM sales GROUP BY customer_id) s on c.id=s.customer_id<br>
WHERE c.store_id = 1<br>
VladL
  • 12,365
  • 10
  • 59
  • 82
  • Kindly include in your answer as well a brief explanation of why this works or what doesn't work in the OP's code such that this answer remedies the problem. :) – WitchGod Feb 24 '14 at 21:49