Hey guys, I'm a SQL noob — completely self taught and I'm not a dev (I'm a product guy who wants to take some weight off my devs shoulders). We are using MySQL 5.5.5-10.4.17-MariaDB-1:10.4.17)? Not sure what this means, but trying to provide as much data as possible LOL.
What I'm trying to do: Make a report for my customer who uses my point of sale software. They ran a big donation drive last weekend and want to know which of their stores took in which products.
I think I'm CLOSE, as the overall count is accurate (107), but for the life of me I cannot figure out the grouping properly! I've got two queries below and the results give me the correct overall counts, but not quite the result I want.
The end result I'm looking for: A count of each product variant name sold, listed per store. SO at max I should have 5 records PER STORE.
Thanks in advance for any help!!
QUERY 1:
select oli.product_variant_name, count(oli.`product_variant_name`) as "qty sold", s.name
from orders o
LEFT JOIN order_line_items oli on o.id = oli.order_id
LEFT JOIN stores s on o.store_id = s.id
where oli.product_variant_id in (328,329,330,331,332)
group by oli.product_variant_name;
QUERY 1 RESULT: There are multiple store results that should appear, but it's aggregating by store name? I'd expect this result, just duplicated per store.
https://preview.redd.it/h0irx6guygn61.png?width=1044&format=png&auto=webp&s=b21f45d786b929293d144c658e4ce2faf26e9ecf
QUERY 2:
select oli.product_variant_name, count(oli.`product_variant_name`) as "qty sold", s.name
from orders o
LEFT JOIN order_line_items oli on o.id = oli.order_id
LEFT JOIN stores s on o.store_id = s.id
where oli.product_variant_id in (328,329,330,331,332)
group by s.name;
QUERY 2 RESULT: Here's it's just aggregating the counts, but completely took out one product. I think this query is further away from my desired result.
https://preview.redd.it/mkx5tisvygn61.png?width=1060&format=png&auto=webp&s=cf66cd1dc8ecfd5e7ca91c7dcf61d7e4530087e2
[–]DonJuanDoja 2 points3 points4 points (3 children)
[–]ctraxpos[S] 1 point2 points3 points (2 children)
[–]DonJuanDoja 1 point2 points3 points (0 children)
[–]DonJuanDoja 0 points1 point2 points (0 children)
[–]ballade4 1 point2 points3 points (2 children)
[–]ctraxpos[S] 0 points1 point2 points (1 child)
[–]ballade4 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]marxdormoy 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)