Answer the question
In order to leave comments, you need to log in
How to merge query in MySQL?
Hello. I'm making a selection from the sales table in order to collect statistics for 12 months, I can't figure out how to combine requests into one.
SELECT
SUM(`price`) as `sales`,
SUM(`seller_salary`) as `salary`,
DATE_FORMAT(`putdate`, '%Y-%m') as period
FROM `ms_sales`
WHERE `putdate` >= DATE_FORMAT(CURRENT_DATE - INTERVAL 11 MONTH, '%Y-%m-01')
GROUP BY period
SELECT
COUNT(`category_id`) as `m`,
DATE_FORMAT(`putdate`, '%Y-%m') as period
FROM `ms_sales`
WHERE `putdate` >= DATE_FORMAT(CURRENT_DATE - INTERVAL 11 MONTH, '%Y-%m-01') AND `category_id` = '1'
GROUP BY period
SELECT
COUNT(`category_id`) as `t`,
DATE_FORMAT(`putdate`, '%Y-%m') as period
FROM `ms_sales`
WHERE `putdate` >= DATE_FORMAT(CURRENT_DATE - INTERVAL 11 MONTH, '%Y-%m-01') AND `category_id` = '2' OR `category_id` = '3'
GROUP BY period
Answer the question
In order to leave comments, you need to log in
First, there are problems with parentheses:
DATE_FORMAT(CURRENT_DATE - INTERVAL 11 MONTH, '%Y-%m-01' AND `category_id` = '1')
AND `category_id` = '2' AND `category_id` = '3'
SELECT
SUM(`price`) as `sales`,
SUM(`seller_salary`) as `salary`,
COUNT(CASE WHEN `category_id` = '1' THEN 1 END) as cat1_count,
COUNT(CASE WHEN `category_id` = '2' OR `category_id` = '3' THEN 1 END) as cat23_count,
DATE_FORMAT(`putdate`, '%Y-%m') as period
FROM `ms_sales`
WHERE `putdate` >= DATE_FORMAT(CURRENT_DATE - INTERVAL 11 MONTH, '%Y-%m-01')
GROUP BY period
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question