I have a MySQL db company
with table department
(the departments in the table are sales and marketing), table campaign
, table product
. And two join tables department_campaign
and department_product
.
Each campaign has 3 promotions every day. The results of the daily promotions are stored in the table promotion_result
. For instance see the rows if the campaign_id = 1:
"created" "campaign_id" "promotion" "product_id"
2020-02-06 09:42:59 1 a 281
2020-02-06 13:35:22 1 b (null)
2020-02-06 17:55:43 1 c (null)
2020-02-07 14:42:59 1 a 350
2020-02-07 14:42:59 1 b 350
2020-02-07 14:42:59 1 c (null)
2020-02-08 14:42:59 1 a (null)
2020-02-08 14:42:59 1 b (null)
2020-02-08 14:42:59 1 c (null)
In the column
"product_id" is the product, which was sold with the promotion at the "created" time.
I would like to have one query to get a list where I can see for each day, which product with which promotion was sold by a department. If there was no product sold on a day then I would like to see to that day only one row with (null). I tried with this query in mysql:
SELECT DATE(pr.created) AS created, pr.campaign_id AS campaign, pr.promotion AS promotion, pr.product_id AS product
FROM department AS dept
LEFT JOIN department_campaign AS dc ON dc.department_id = dept.id
LEFT JOIN department_products AS dp ON dp.department_id = dept.id
LEFT JOIN promotion_result AS pr ON dc.campaign_id = pr.campaign_id
WHERE dept.id = 2 AND pr.campaign_id = 1
GROUP BY DATE(pr.created), product_id, promotion
ORDER BY DATE(pr.created) ASC, promotion ASC
The result:
"created" "campaign" "promotion" "product"
2020-02-06 1 a 281
2020-02-06 1 b (null)
2020-02-06 1 c (null)
2020-02-07 1 a 350
2020-02-07 1 b 350
2020-02-07 1 c (null)
2020-02-08 1 a (null)
2020-02-08 1 b (null)
2020-02-08 1 c (null)
If I add AND dp.product_id = pr.product_id
to the where
then I get only the days where products were sold but not the day(s) whitout sales:
"created" "campaign" "promotion" "product"
2020-02-06 1 a 281
2020-02-07 1 a 350
2020-02-07 1 b 350
What expected:
"created" "campaign" "promotion" "product"
2020-02-06 1 a 281
2020-02-07 1 a 350
2020-02-07 1 b 350
2020-02-08 1 (null) (null)
In this case I could see each day with sold products per promotion and also the days without sales ("product" = (null) and "promotion" = (null))
Can anybody help me with this problem?