I'm trying to 'bin' all of my customers based on the number of items purchased, and display the counts of each bin. I'm trying to see how many people(account_id) purchased one item, how many purchased two items, all the way through nine items, and then ten or more.
Here's the query I'm using - for what its worth, I'd expect the query to do a full-table-scan on sales in order to generate the results, but the whole process takes forever!
I'm coming from an Oracle background and I wrote the query as I would in Oracle.
select thecnt
, count(*)
from (select count(*)
, case when count(*) >= 10 then 'tenormore' else cast(count(*) as char) end thecnt
from sales
where created >= SUBDATE( CURRENT_DATE(), INTERVAL 60 DAY )
group by account_id) sub
group by thecnt
order by thecnt;
are there any gotchas in mysql when dealing with subqueries?
explain plan
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2143248 | 100.00 | Using temporary; Using filesort |
| 2 | DERIVED | sales | range | created | created | 8 | NULL | 2012492 | 100.00 | Using where; Using index; Using temporary; Using filesort |
+----+-------------+-------------------+-------+---------------+---------+---------+------+---------+----------+-----------------------------------------------------------+
2 rows in set, 1 warning (1 hour 4 min 6.14 sec)
mysql> describe sales;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| account_id | char(36) | NO | PRI | NULL | |
| created | datetime | NO | MUL | NULL | |
| histogram_value | bigint(20) unsigned | NO | PRI | NULL | |
+-----------------+---------------------+------+-----+---------+-------+