Skip to content

Commit 8454a5c

Browse files
authored
MEDIUM
1 parent ad104af commit 8454a5c

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

1070. Product Sales Analysis III.sql

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,75 @@
1+
-- Problem:
2+
3+
/*
4+
Table: Sales
5+
6+
+-------------+-------+
7+
| Column Name | Type |
8+
+-------------+-------+
9+
| sale_id | int |
10+
| product_id | int |
11+
| year | int |
12+
| quantity | int |
13+
| price | int |
14+
+-------------+-------+
15+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
16+
product_id is a foreign key (reference column) to Product table.
17+
Each row of this table shows a sale on the product product_id in a certain year.
18+
Note that the price is per unit.
19+
20+
21+
Table: Product
22+
23+
+--------------+---------+
24+
| Column Name | Type |
25+
+--------------+---------+
26+
| product_id | int |
27+
| product_name | varchar |
28+
+--------------+---------+
29+
product_id is the primary key (column with unique values) of this table.
30+
Each row of this table indicates the product name of each product.
31+
32+
33+
Write a solution to select the product id, year, quantity, and price for the first year of every product sold.
34+
35+
Return the resulting table in any order.
36+
37+
The result format is in the following example.
38+
39+
40+
41+
Example 1:
42+
43+
Input:
44+
Sales table:
45+
+---------+------------+------+----------+-------+
46+
| sale_id | product_id | year | quantity | price |
47+
+---------+------------+------+----------+-------+
48+
| 1 | 100 | 2008 | 10 | 5000 |
49+
| 2 | 100 | 2009 | 12 | 5000 |
50+
| 7 | 200 | 2011 | 15 | 9000 |
51+
+---------+------------+------+----------+-------+
52+
Product table:
53+
+------------+--------------+
54+
| product_id | product_name |
55+
+------------+--------------+
56+
| 100 | Nokia |
57+
| 200 | Apple |
58+
| 300 | Samsung |
59+
+------------+--------------+
60+
Output:
61+
+------------+------------+----------+-------+
62+
| product_id | first_year | quantity | price |
63+
+------------+------------+----------+-------+
64+
| 100 | 2008 | 10 | 5000 |
65+
| 200 | 2011 | 15 | 9000 |
66+
+------------+------------+----------+-------+
67+
*/
68+
69+
-------------------------------------------------------------------------------
70+
71+
-- Solution:
72+
173
SELECT product_id,year AS first_year,quantity,price
274
FROM Sales
375
WHERE (product_id, year) IN

0 commit comments

Comments
 (0)