-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql11_Nvidia.sql
More file actions
29 lines (23 loc) · 1.63 KB
/
Copy pathsql11_Nvidia.sql
File metadata and controls
29 lines (23 loc) · 1.63 KB
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
/* Find the number of transactions that occurred for each product. Output the product name along with the corresponding number
of transactions and order records by the product id in ascending order. You can ignore products without transactions.*/
CREATE TABLE excel_sql_inventory_data (product_id INT,product_name VARCHAR(50),product_type VARCHAR(50),
unit VARCHAR(20),price_unit FLOAT,wholesale FLOAT,current_inventory INT);
INSERT INTO excel_sql_inventory_data (product_id, product_name, product_type, unit, price_unit, wholesale, current_inventory)
VALUES(1, 'strawberry', 'produce', 'lb', 3.28, 1.77, 13),(2, 'apple_fuji', 'produce', 'lb', 1.44, 0.43, 2),
(3, 'orange', 'produce', 'lb', 1.02, 0.37, 2),(4, 'clementines', 'produce', 'lb', 1.19, 0.44, 44),
(5, 'blood_orange', 'produce', 'lb', 3.86, 1.66, 19);
CREATE TABLE excel_sql_transaction_data (transaction_id INT PRIMARY KEY,time DATETIME,product_id INT);
INSERT INTO excel_sql_transaction_data (transaction_id, time, product_id)
VALUES(153, '2016-01-06 08:57:52', 1),(91, '2016-01-07 12:17:27', 1),(31, '2016-01-05 13:19:25', 1),
(24, '2016-01-03 10:47:44', 3),(4, '2016-01-06 17:57:42', 3),(163, '2016-01-03 10:11:22', 3),(92, '2016-01-08 12:03:20', 2),
(32, '2016-01-04 19:37:14', 4),(253, '2016-01-06 14:15:20', 5),(118, '2016-01-06 14:27:33', 5);
with cte as
(Select a.product_id,a.product_name,b.transaction_id,b.time
from excel_sql_inventory_data a left join
excel_sql_transaction_data b
on a.product_id=b.product_id)
Select product_id,product_name,count(transaction_id) as transaction_count
from cte
group by product_id,product_name
having count(transaction_id)>0
order by product_id asc;