數(shù)據(jù)導入:
SQL Schema:
Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10)); Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int); Create table If Not Exists Items (item_id int, item_brand varchar(10)); Truncate table Users; insert into Users (user_id, join_date, favorite_brand) values ('1', '2018-01-01', 'Lenovo'); insert into Users (user_id, join_date, favorite_brand) values ('2', '2018-02-09', 'Samsung'); insert into Users (user_id, join_date, favorite_brand) values ('3', '2018-01-19', 'LG'); insert into Users (user_id, join_date, favorite_brand) values ('4', '2018-05-21', 'HP'); Truncate table Orders; insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2'); insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2018-08-02', '2', '1', '3'); insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3'); insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2018-08-04', '1', '4', '2'); insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2018-08-04', '1', '3', '4'); insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4'); Truncate table Items; insert into Items (item_id, item_brand) values ('1', 'Samsung'); insert into Items (item_id, item_brand) values ('2', 'Lenovo'); insert into Items (item_id, item_brand) values ('3', 'LG'); insert into Items (item_id, item_brand) values ('4', 'HP');
Pandas Schema:
data = [[1, '2018-01-01', 'Lenovo'], [2, '2018-02-09', 'Samsung'], [3, '2018-01-19', 'LG'], [4, '2018-05-21', 'HP']] Users = pd.DataFrame(data, columns=['user_id', 'join_date', 'favorite_brand']).astype({'user_id':'Int64', 'join_date':'datetime64[ns]', 'favorite_brand':'object'}) data = [[1, '2019-08-01', 4, 1, 2], [2, '2018-08-02', 2, 1, 3], [3, '2019-08-03', 3, 2, 3], [4, '2018-08-04', 1, 4, 2], [5, '2018-08-04', 1, 3, 4], [6, '2019-08-05', 2, 2, 4]] Orders = pd.DataFrame(data, columns=['order_id', 'order_date', 'item_id', 'buyer_id', 'seller_id']).astype({'order_id':'Int64', 'order_date':'datetime64[ns]', 'item_id':'Int64', 'buyer_id':'Int64', 'seller_id':'Int64'}) data = [[1, 'Samsung'], [2, 'Lenovo'], [3, 'LG'], [4, 'HP']] Items = pd.DataFrame(data, columns=['item_id', 'item_brand']).astype({'item_id':'Int64', 'item_brand':'object'})
表結構:
表:?Users
+----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | join_date | date | | favorite_brand | varchar | +----------------+---------+
????????user_id 是此表主鍵(具有唯一值的列)。
????????表中描述了購物網(wǎng)站的用戶信息,用戶可以在此網(wǎng)站上進行商品買賣。
表:?Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | order_date | date | | item_id | int | | buyer_id | int | | seller_id | int | +---------------+---------+
????????order_id 是此表主鍵(具有唯一值的列)。
????????item_id 是 Items 表的外鍵(reference 列)。
????????(buyer_id,seller_id)是 User 表的外鍵。
表:Items
+---------------+---------+ | Column Name | Type | +---------------+---------+ | item_id | int | | item_brand | varchar | +---------------+---------+
????????item_id 是此表的主鍵(具有唯一值的列)。
編寫解決方案找出每個用戶的注冊日期和在?2019
?年作為買家的訂單總數(shù)。
以?任意順序?返回結果表。
查詢結果格式如下。
示例 1:
輸入: Users 表: +---------+------------+----------------+ | user_id | join_date | favorite_brand | +---------+------------+----------------+ | 1 | 2018-01-01 | Lenovo | | 2 | 2018-02-09 | Samsung | | 3 | 2018-01-19 | LG | | 4 | 2018-05-21 | HP | +---------+------------+----------------+ Orders 表: +----------+------------+---------+----------+-----------+ | order_id | order_date | item_id | buyer_id | seller_id | +----------+------------+---------+----------+-----------+ | 1 | 2019-08-01 | 4 | 1 | 2 | | 2 | 2018-08-02 | 2 | 1 | 3 | | 3 | 2019-08-03 | 3 | 2 | 3 | | 4 | 2018-08-04 | 1 | 4 | 2 | | 5 | 2018-08-04 | 1 | 3 | 4 | | 6 | 2019-08-05 | 2 | 2 | 4 | +----------+------------+---------+----------+-----------+ Items 表: +---------+------------+ | item_id | item_brand | +---------+------------+ | 1 | Samsung | | 2 | Lenovo | | 3 | LG | | 4 | HP | +---------+------------+ 輸出: +-----------+------------+----------------+ | buyer_id | join_date | orders_in_2019 | +-----------+------------+----------------+ | 1 | 2018-01-01 | 1 | | 2 | 2018-02-09 | 2 | | 3 | 2018-01-19 | 0 | | 4 | 2018-05-21 | 0 | +-----------+------------+----------------+
預備知識
????????本題使用到的 MySQL 函數(shù)的說明:
ifnull(x1, x2) :如果 x1 為 NULL, 返回 x2,否則返回 x1。
方法一:left join 和 group by
思路
????????首先根據(jù) Orders 表計算每個用戶的訂單數(shù),通過 buyer_id 和 Users 表的 user_id 將兩份數(shù)據(jù)結合,找到所有用戶的注冊時間和訂單的數(shù)量。
????????注意:用戶訂單的數(shù)量可能會 0,需要使用 ifnull 函數(shù)特殊處理這種數(shù)據(jù)。
算法
????????使用 Orders 表計算每個用戶的產(chǎn)品數(shù)。使用 group by 聚合每個用戶的購買記錄。使用 between 篩選出時間為 2019 年的數(shù)據(jù)。使用 count(order_id) 計算出每個用戶的訂單數(shù)。文章來源:http://www.zghlxwxcb.cn/news/detail-687685.html
Mysql
select buyer_id, count(order_id) cnt?
from Orders
where order_date between '2019-01-01' and '2019-12-31'
group by buyer_id
????????使用 Users 表得到所有用戶及其注冊時間。并使用 left join,通過 user_id 和第一步的數(shù)據(jù)連接,求每個用戶的訂單數(shù)。 如果一個用戶沒有任何訂單,那么第一步的數(shù)據(jù)中不會有這個用戶的數(shù)據(jù),最后的 orders_in_2019 會顯示為 null,所以我們還需要使用 ifnull,如果數(shù)據(jù)為 null,將其改為 0。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-687685.html
Mysql代碼
select Users.user_id as buyer_id, join_date, ifnull(UserBuy.cnt, 0) as orders_in_2019
from Users
left join (
? ? select buyer_id, count(order_id) cnt?
? ? from Orders
? ? where order_date between '2019-01-01' and '2019-12-31'
? ? group by buyer_id
) UserBuy
on Users.user_id = UserBuy.buyer_id
到了這里,關于leecode 數(shù)據(jù)庫:1158. 市場分析 I的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!