I have to get the returns for products, which share a common_id, but they also have unique IDs which is some coded number including their color and sizes,
Now i have come up with the query for this, I am joining the two tables to get the general information, so the totals for a product with their common ID,
and I have another CTE to get the information of the products sharing the common ID, so one row for the table would include the common ID, the totals (sales, returns, order and return quantity) for that common ID, then I also have a list of the skus sharing that ID, and other lists with the same info, (sales, returns, order and return quantity) of each sku in the skus list.
It's an expandable table where the original row is the product and expanding it gives the different sku sizes with individual info that totals up. Below is my query,
WITH SkuStatsCTE AS (
SELECT
DISTINCT article_no,
common_id,
SUM(o.return_price) AS total_return_price,
SUM(CASE WHEN oi.return_price IS NOT NULL THEN 1 ELSE 0 END) AS return_quantity
FROM
orders o
join order_items oi on oi.order_id=o.id
GROUP BY common_id, article_no
)
SELECT
main.common_id AS common_id,
SUBSTRING_INDEX(main.variant, ' | ', 1) AS color,
SUM(main.price) AS total_order_price,
SUM(main.return_price) AS total_return_price,
COUNT(DISTINCT o.id) AS order_count,
SUM(CASE
WHEN main.return_price IS NOT NULL THEN 1
ELSE 0
END) AS total_return_quantity,
AVG(CASE
WHEN main.return_price IS NOT NULL THEN 1.0
ELSE 0.0
END) * 100 AS avg_return_percentage,
GROUP_CONCAT(DISTINCT main.article_no) AS skus,
GROUP_CONCAT(DISTINCT main.product_name) AS product_names,
GROUP_CONCAT(DISTINCT main.variant) AS variants,
GROUP_CONCAT(sku_stats.total_return_price) AS return_prices,
GROUP_CONCAT(sku_stats.return_quantity) AS return_quantities
FROM
order_items main
JOIN orders o ON o.id = main.order_id
left JOIN SkuStatsCTE sku_stats ON main.article_no = sku_stats.article_no
AND main.common_id = sku_stats.common_id
WHERE
YEAR(o.created_at) = 2023
AND MONTH(o.created_at) = 7
GROUP BY main.common_id , color
ORDER BY total_return_quantity DESC;
Now, the issue is, I get some skewed results, specifically for my return_prices and return_quantities. The values are repeated, (return_prices for one sku, is repeated depending on the return quantity for that sku) when I use DISTINCT in the GROUP_CONCAT function however, it works, but only for the sku sizes that don't have identical return_prices or quantities.
How can i get the individual values for each of the skus in the list without them repeating? Below is how the table looks like. Table