-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-2.sql
More file actions
56 lines (53 loc) · 1.48 KB
/
task-2.sql
File metadata and controls
56 lines (53 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
-- Общее количество покупок, совершенных каждым клиентом --
SELECT
c.NameCustomer,
COUNT(o.OrderID) AS TotalPurchases
FROM
CLIENTS c
LEFT JOIN
ORDERS o ON c.CustomerID = o.CustomerID
GROUP BY
c.CustomerID, c.NameCustomer
ORDER BY
TotalPurchases DESC;
-- Топ-5 самых покупаемых товаров, с количеством их покупок --
SELECT
p.ProductName,
COUNT(o.ProductID) AS TotalPurchases
FROM
PRODUCTS p
LEFT JOIN
ORDERS o ON p.ProductID = o.ProductID
GROUP BY
p.ProductID, p.ProductName
ORDER BY
TotalPurchases DESC
LIMIT 5;
-- Анализ предпочтительных магазинов для каждого клиента на --
-- основе количества покупок, совершенных в каждом магазине: --
SELECT
c.NameCustomer,
s.StoreCity,
COUNT(o.OrderID) AS TotalPurchases
FROM
CLIENTS c
LEFT JOIN
ORDERS o ON c.CustomerID = o.CustomerID
LEFT JOIN
STORES s ON o.StoreID = s.StoreID
GROUP BY
c.CustomerID, c.NameCustomer, s.StoreCity
ORDER BY
c.NameCustomer, TotalPurchases DESC;
-- Среднее количество покупок каждого клиента --
SELECT
c.NameCustomer,
AVG(o.PurchaseAmount) AS AveragePurchaseAmount
FROM
CLIENTS c
LEFT JOIN
ORDERS o ON c.CustomerID = o.CustomerID
GROUP BY
c.CustomerID, c.NameCustomer
ORDER BY
AveragePurchaseAmount DESC;