Answer the question
In order to leave comments, you need to log in
How to combine two queries into one grouping by condition in SQL?
Hello!
Please tell me how this can be done:
There is a request
SELECT Count(sales.id), sale_group.code AS group
FROM sales
INNER JOIN sale_group
ON sales.group_id = sale_group.id
GROUP BY sale_group.code
, which counts the number of orders for each group. How can I make it so that the result also displays groups for which there are no orders? SELECT Count(sales.id), sale_group.code AS group
FROM sales
INNER JOIN sale_group
ON sales.group_id = sale_group.id
WHERE sales.path
LIKE '43/44/45/%' AND sales.id != 45
GROUP BY sale_group.code
SELECT Count(id FROM sales WHERE path
LIKE '43/44/45/%' AND id != 45), sale_group.code AS group
FROM sales
RIGHT JOIN sale_group
ON sales.group_id = sale_group.id
GROUP BY sale_group.code
Answer the question
In order to leave comments, you need to log in
SELECT Count(s.id), sg.code AS group
FROM sale_group sg
LEFT JOIN sales s ON s.group_id = sg.id AND s.path LIKE '43/44/45/%' AND s.id <> 45
GROUP BY sg.code
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question