1. Zamówienia:
- order_date datetime NIE NULL
- user_id int NIE NULL
- order_id int NIE NULL
2. Towary:
- order_id int NIE NULL
- model int NIE NULL
- cat_1 varchar(50) NIE NULL
- price int NIE NULL
- quantity int NIE NULL
Wartości w cat_1:
- Odzież
- Obuwie
- Kosmetyki
- Artykuły domowe
- Zabawki
Wyświetlić liczbę zamówień i "liczbę potrzebnych produktów w kategorii w zamówieniu" za 2021 rok w kategoriach "Odzież" i za 2022 rok w kategorii "Obuwie"
(ostateczny widok: 2 wiersze. Pola: rok, liczba produktów, liczba zamówień)
with cte as(
select
year(o.order_date) as year_orders,
g.cat_1,
count(distinct o.order_id) as count_orders,
sum(g.quantity) as count_quan
from Orders o
join Goods g on o.order_id = g.order_id
where g.cat_1 = 'Odzież' and Year(o.order_date) = 2021 or
g.cat_1 = 'Obuwie' and Year(o.order_date) = 2022
group by year_orders, g.cat_1
)
select year_orders, count_quan, count_orders
from cte