This is what I have:
customer
--------
customerId INT(10)
name TINYTEXT
phone VARCHAR(10)
...
order
-----
orderId int(10)
customerId INT(10)
dateCompleted DATETIME
...
This is what I need:
custId name thisMo lastMo last3Mos monthsThisYear ordersPerMonth
------ ---- ------ ------ -------- -------------- --------------
1 John 5 10 28 2019-01,2019-02,2019-03,.. 7,11,9..
2 Bill 9 15 33 2019-01,2019-02,2019-03,.. 12,10,14..
3 Jack 3 8 19 2019-01,2019-02,2019-03,.. 3,7,5..
Trying to generate a table showing customers, number of orders they've placed this month, last month, and last 3 months, along with a spark line chart showing number of orders per month for the entire past year (hence the comma separated lists of months and orders per month)
Currently, they're using a single query to get all customers, then each customer is looped through to generate the table and additional queries are made to get the numbers. The queries to get the csv are as follows:
SELECT GROUP_CONCAT(`count` SEPARATOR ',') AS `countCsv`
FROM (
SELECT SUM(IF(`customerId` = {$row['customerId']}, 1, 0)) AS `count`
FROM `order`
WHERE DATE(`dateCompleted`) BETWEEN DATE('{$sqlStartDate}') AND DATE('{$sqlEndDate}')
GROUP BY DATE_FORMAT(`dateCompleted`, '%Y-%m')
) `Order`
SELECT GROUP_CONCAT(`month` SEPARATOR ',') AS `monthCsv`
FROM (
SELECT DATE_FORMAT(`dateCompleted`, '%Y-%m') AS `month`
FROM `order`
WHERE DATE(`dateCompleted`) BETWEEN DATE('{$sqlStartDate}') AND DATE('{$sqlEndDate}')
GROUP BY DATE_FORMAT(`dateCompleted`, '%Y-%m')
) `Order`
…but this is resulting in 5 extra queries per row per page load and it's taking forever. I've been trying all day (8-1/2 hours now) to search and extrapolate from similar questions and I just can't get anything even close to working in a single query.
Edit – added customerId to order table to show customerId as the relationship between the two tables.
Best Answer
Step 1: Get the data without the complexity:
(Fox and try that query now.)
Step 2: Compute the counts:
(Try that query now.)
Step 3: Use that as a "derived" table to build the ultimate table.
There are several holes, but maybe this gives you some useful clues.
If you are using MySQL 8.0 or MariaDB 10.2, use
WITH
instead of a temp table.