Posted by Martin, Feb. 27, 2023, 10:01 a.m.
Q94 - what is the problem
I created a query for Q94 but I don't seem to get the answer, can someone please help?
WITH a AS(
SELECT
product_id,
sum(unit_price_usd * qty) AS sales
FROM orders
WHERE
EXTRACT(YEAR FROM order_dt) = '2021'
AND EXTRACT(MONTH FROM order_dt) = '08'
GROUP BY 1
ORDER by sales DESC
LIMIT 3
),
b AS(
SELECT
product_id,
sum(unit_price_usd * qty) AS sales
FROM orders
WHERE
EXTRACT(YEAR FROM order_dt) = '2021'
AND EXTRACT(MONTH FROM order_dt) = '08'
GROUP BY 1
ORDER by sales ASC
LIMIT 3
)
SELECT product_id, 'top' AS category FROM a
UNION
SELECT product_id, 'bottom' AS category FROM b
ORDER BY category
;