-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-8.sql
More file actions
33 lines (32 loc) · 748 Bytes
/
task-8.sql
File metadata and controls
33 lines (32 loc) · 748 Bytes
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
-- Решение №8 --
WITH TopProducts AS (
SELECT
o.ProductID,
p.ProductName,
SUM(o.PurchaseAmount) AS TotalSales
FROM
ORDERS o
JOIN
PRODUCTS p ON o.ProductID = p.ProductID
WHERE
o.OrderDate >= NOW() - INTERVAL '7 months' -- Не было данных на более ранние месяца!
GROUP BY
o.ProductID, p.ProductName
ORDER BY
TotalSales DESC
LIMIT 5
)
SELECT
tp.ProductID,
tp.ProductName,
tp.TotalSales,
c.CustomerID,
c.NameCustomer
FROM
TopProducts tp
JOIN
ORDERS o ON tp.ProductID = o.ProductID
JOIN
CLIENTS c ON o.CustomerID = c.CustomerID
ORDER BY
tp.TotalSales DESC, tp.ProductID, c.CustomerID;