-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProducts Sales Per City(Solution-1,2).txt
48 lines (44 loc) · 1.13 KB
/
Products Sales Per City(Solution-1,2).txt
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
---- Products Sales Per City(Solution_1)
SELECT invoice.city_name,
product.product_name,
invoice_item.line_total_price
FROM invoice_item as invoice_item
JOIN product as product
ON invoice_item.product_id = product.id
JOIN (
SELECT invoice.id,
customer.city_name
FROM invoice as invoice
JOIN (
SELECT customer.id,city.city_name
FROM customer as customer
LEFT JOIN city as city
ON customer.city_id = city.id
)as customer
ON invoice.customer_id = customer.id
) as invoice
ON invoice_item.invoice_id = invoice.id
ORDER BY invoice_item.line_total_price DESC;
---- Products Sales Per City(Solution_2)
SELECT
CI.city_name,
PR.product_name,
ROUND(SUM(INV_I.line_total_price), 2) AS tot
FROM
city as CI,
customer as CU,
invoice as INV,
invoice_item as INV_I,
product as PR
WHERE
CI.id = CU.city_id
AND CU.id = INV.customer_id
AND INV.id = INV_I.invoice_id
AND INV_I.product_id = PR.id
GROUP BY
CI.city_name,
PR.product_name
ORDER BY
tot DESC,
CI.city_name,
PR.product_name ;