I’m dealing with two tables and I’m looking for assistance with a LEFT JOIN query to group the data by date. Here’s how my database is structured:
CREATE TABLE feedback (
id INT(11) NOT NULL AUTO_INCREMENT,
store_id INT(11),
transaction_id CHAR(255),
item_id CHAR(32),
feedback_date INT(11),
PRIMARY KEY (id)
);
INSERT INTO feedback (store_id, transaction_id, item_id, feedback_date)
VALUES
(15, '200', '2000', 1466190000),
(15, '200', '2000', 1466276400),
(15, '200', '2000', 1466462800),
(25, '900', '9000', 1466249200);
CREATE TABLE labels (
id INT(11) NOT NULL AUTO_INCREMENT,
store_id INT(11),
transaction_id CHAR(255),
item_id CHAR(32),
label_date INT(11) NULL,
PRIMARY KEY (id)
);
INSERT INTO labels (store_id, transaction_id, item_id, label_date)
VALUES
(15, '200', '2000', 1466449200),
(15, '200', '2000', NULL),
(15, '200', '4000', NULL),
(25, '900', '9000', 1469449200);
I want to generate a report reflecting daily data regarding total feedback counts, including numbers for labeled and unlabeled items. Here’s my current query:
SELECT
DATE_FORMAT(FROM_UNIXTIME(f.feedback_date), "%d.%m.%Y") AS submission_date,
f.store_id,
COUNT(*) as total_items,
COUNT(*) as labeled_items
FROM
feedback AS f
LEFT JOIN labels as l
ON f.store_id = l.store_id AND
f.transaction_id = l.transaction_id AND
f.item_id = l.item_id
WHERE
l.label_date IS NOT NULL
GROUP BY f.store_id, f.transaction_id, f.item_id
ORDER BY submission_date ASC;
I expect the output to look like this:
| submission_date | store_id | total_items | labeled_items |
|-----------------|----------|-------------|---------------|
| 17.06.2016 | 15 | 3 | 1 |
| 18.06.2016 | 25 | 1 | 1 |
What adjustments do I need to make to this query so that I can obtain the accurate totals for both total and labeled items?