-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalesAnalyst1070.sql
41 lines (37 loc) · 1.3 KB
/
salesAnalyst1070.sql
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
/*
1070. Product Sales Analysis III: https://leetcode.com/problems/product-sales-analysis-iii/description/ Medium Amazon Interview Question
Table: Sales
+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+
(sale_id, year) is the primary key of this table.
product_id is a foreign key to Product table.
Each row of this table shows a sale on the product product_id in a certain year.
Note that the price is per unit.
Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+
product_id is the primary key of this table.
Each row of this table indicates the product name of each product.
Write an SQL query that selects the product id, year, quantity, and price for the first year of every product sold.
------------------------------------------
*/
-- Solution Beats 55% in runtime:
SELECT product_id, year as first_year, quantity, price
FROM (SELECT s.product_id, s.year, s.quantity, s.price,
dense_rank() OVER(PARTITION BY s.product_id ORDER BY s.year) as rn
FROM sales s
LEFT JOIN product p
ON s.product_id = p.product_id
) t
WHERE rn = 1