|
| 1 | +-- 1069. Product Sales Analysis II |
| 2 | +--Table: Sales |
| 3 | +-- |
| 4 | +--+-------------+-------+ |
| 5 | +--| Column Name | Type | |
| 6 | +--+-------------+-------+ |
| 7 | +--| sale_id | int | |
| 8 | +--| product_id | int | |
| 9 | +--| year | int | |
| 10 | +--| quantity | int | |
| 11 | +--| price | int | |
| 12 | +--+-------------+-------+ |
| 13 | +--sale_id is the primary key of this table. |
| 14 | +--product_id is a foreign key to Product table. |
| 15 | +--Note that the price is per unit. |
| 16 | +--Table: Product |
| 17 | +-- |
| 18 | +--+--------------+---------+ |
| 19 | +--| Column Name | Type | |
| 20 | +--+--------------+---------+ |
| 21 | +--| product_id | int | |
| 22 | +--| product_name | varchar | |
| 23 | +--+--------------+---------+ |
| 24 | +--product_id is the primary key of this table. |
| 25 | +-- |
| 26 | +-- |
| 27 | +--Write an SQL query that reports the total quantity sold for every product id. |
| 28 | +-- |
| 29 | +--The query result format is in the following example: |
| 30 | +-- |
| 31 | +--Sales table: |
| 32 | +--+---------+------------+------+----------+-------+ |
| 33 | +--| sale_id | product_id | year | quantity | price | |
| 34 | +--+---------+------------+------+----------+-------+ |
| 35 | +--| 1 | 100 | 2008 | 10 | 5000 | |
| 36 | +--| 2 | 100 | 2009 | 12 | 5000 | |
| 37 | +--| 7 | 200 | 2011 | 15 | 9000 | |
| 38 | +--+---------+------------+------+----------+-------+ |
| 39 | +-- |
| 40 | +--Product table: |
| 41 | +--+------------+--------------+ |
| 42 | +--| product_id | product_name | |
| 43 | +--+------------+--------------+ |
| 44 | +--| 100 | Nokia | |
| 45 | +--| 200 | Apple | |
| 46 | +--| 300 | Samsung | |
| 47 | +--+------------+--------------+ |
| 48 | +-- |
| 49 | +--Result table: |
| 50 | +--+--------------+----------------+ |
| 51 | +--| product_id | total_quantity | |
| 52 | +--+--------------+----------------+ |
| 53 | +--| 100 | 22 | |
| 54 | +--| 200 | 15 | |
| 55 | +--+--------------+----------------+ |
| 56 | + |
| 57 | +SELECT product_id, SUM(quantity) AS total_quantity FROM Sales GROUP BY product_id; |
0 commit comments