1. 哪一位顧客是消費大戶??

在此輸入你的程式碼

library(rmarkdown)
library(tinytex)
## Warning: 套件 'tinytex' 是用 R 版本 4.3.3 來建造的
library(dplyr)
## Warning: 套件 'dplyr' 是用 R 版本 4.3.3 來建造的
## 
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
## 
##     filter, lag
## 下列物件被遮斷自 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
## Warning: 套件 'readxl' 是用 R 版本 4.3.3 來建造的
#1.讀取 Excel 數據
Categories <- read_excel("C:/Users/User/OneDrive/桌面/R/Categories.xlsx")
Customers <- read_excel("C:/Users/User/OneDrive/桌面/R/Customers.xlsx")
Employees <- read_excel("C:/Users/User/OneDrive/桌面/R/Employees.xlsx")
OrderDetails <- read_excel("C:/Users/User/OneDrive/桌面/R/OrderDetails.xlsx")
Orders <- read_excel("C:/Users/User/OneDrive/桌面/R/Orders.xlsx")
Products <- read_excel("C:/Users/User/OneDrive/桌面/R/Products.xlsx")
Shippers <- read_excel("C:/Users/User/OneDrive/桌面/R/Shippers.xlsx")
Suppliers <- read_excel("C:/Users/User/OneDrive/桌面/R/Suppliers.xlsx")

#2.創建 price 數據框
price <- data.frame(ProductID = Products$ProductID, Price = Products$Price)

#3.合併 OrderDetails 和 Orders 以獲取顧客資訊
order_info <- merge(OrderDetails, Orders, by = "OrderID") %>%
  merge(price, by = "ProductID")  # 獲取價格

#4.計算每筆訂單的總消費
order_info <- order_info %>%
  mutate(TotalPrice = Quantity * Price)

#5.計算每位顧客的總消費金額
customer_spending <- order_info %>%
  group_by(CustomerID) %>%
  summarise(TotalSpent = sum(TotalPrice, na.rm = TRUE), .groups = "drop")

#6.找到消費最多的客戶
top_customer <- customer_spending %>%
  filter(TotalSpent == max(TotalSpent, na.rm = TRUE))
  
#7.獲取該客戶的 ID 和消費金額
top_customer_id <- top_customer$CustomerID
#8.獲取消費金額
total_spent_amount <- top_customer$TotalSpent  

  

#9.找到該客戶購買過的所有產品
top_customer_orders <- order_info %>%
  filter(CustomerID == top_customer_id) %>%
  select(CustomerID, ProductID, Quantity, Price, TotalPrice)

#10.檢查結果
print("消費最多的客戶:")
## [1] "消費最多的客戶:"
print(top_customer)
## # A tibble: 1 × 2
##   CustomerID TotalSpent
##        <dbl>      <dbl>
## 1         20     35631.

2. 這一位顧客最近買過什麼產品??

在此輸入你的程式碼

#獲取該客戶的 ID
top_customer_id <- top_customer$CustomerID

#找到該客戶購買過的所有產品
top_customer_orders <- order_info %>%
  filter(CustomerID == top_customer_id) %>%
  select(CustomerID, ProductID, Quantity, Price, TotalPrice, OrderDate)
print("這一位顧客最近買過什麼產品?")
## [1] "這一位顧客最近買過什麼產品?"
print(top_customer_orders)
##    CustomerID ProductID Quantity  Price TotalPrice  OrderDate
## 1          20         2       50  19.00     950.00 1996-07-17
## 2          20         5       65  21.35    1387.75 1996-07-17
## 3          20         5       32  21.35     683.20 1996-12-13
## 4          20        11       30  21.00     630.00 1997-02-11
## 5          20        16       60  17.45    1047.00 1996-07-23
## 6          20        16       21  17.45     366.45 1997-01-03
## 7          20        17       45  39.00    1755.00 1997-01-30
## 8          20        18        9  62.50     562.50 1996-12-13
## 9          20        21        5  10.00      50.00 1996-11-29
## 10         20        21       50  10.00     500.00 1997-01-30
## 11         20        23       60   9.00     540.00 1997-01-02
## 12         20        24       28   4.50     126.00 1996-07-23
## 13         20        28       13  45.60     592.80 1996-11-29
## 14         20        29       14 123.79    1733.06 1996-12-13
## 15         20        30       60  25.89    1553.40 1996-07-23
## 16         20        31       60  12.50     750.00 1996-12-23
## 17         20        32        6  32.00     192.00 1996-07-17
## 18         20        33       60   2.50     150.00 1996-12-13
## 19         20        35       40  18.00     720.00 1996-12-23
## 20         20        38       20 263.50    5270.00 1996-11-11
## 21         20        41       13   9.65     125.45 1996-11-11
## 22         20        44       77  19.45    1497.65 1996-11-11
## 23         20        46       45  12.00     540.00 1996-12-23
## 24         20        48       70  12.75     892.50 1997-01-03
## 25         20        54       80   7.45     596.00 1997-02-11
## 26         20        56       30  38.00    1140.00 1997-01-30
## 27         20        57       25  19.50     487.50 1996-11-29
## 28         20        59       70  55.00    3850.00 1997-01-30
## 29         20        63       65  43.90    2853.50 1997-01-02
## 30         20        64       35  33.25    1163.75 1996-11-29
## 31         20        65       10  21.05     210.50 1996-11-11
## 32         20        66       60  17.00    1020.00 1997-02-11
## 33         20        72       24  34.80     835.20 1996-12-23
## 34         20        74       36  10.00     360.00 1996-07-23
## 35         20        74       50  10.00     500.00 1996-12-13
#在此輸入你對本題的解讀
#合併 OrderDetails 和 Orders 找到總消費額最高的客戶,獲取該客戶的 ID 和消費金額抓出這位客戶購買的產品、價錢和數量,列出所有購買的產品

3. 這一位顧客最近的消費金額是多少??

在此輸入你的程式碼

#找到該客戶購買過的所有產品

top_customer_orders <- order_info %>%
  filter(CustomerID == top_customer_id) %>%
  select(CustomerID, ProductID, Quantity, Price, TotalPrice, OrderDate)

  
print("這一位顧客最近的消費金額是多少?")
## [1] "這一位顧客最近的消費金額是多少?"
print(total_spent_amount)
## [1] 35631.21
#在此輸入你對本題的解讀

#找到該客戶購買過的所有產品的詳細信息,包括顧客ID、產品ID、數量、單價、總價和訂單日期,輸出客戶的消費金額。

4. 這一位顧客最近一次消費是什麼時候??

# 在此輸入你的程式碼
#找到該顧客最近的消費金額
recent_purchase <- top_customer_orders %>%
  arrange(desc(OrderDate)) %>%  #按 OrderDate 降序排列
  slice(1)  # 獲取最近的一筆訂單
  
recent_date <- recent_purchase$OrderDate
print("這一位顧客最近一次消費是什麼時候?")
## [1] "這一位顧客最近一次消費是什麼時候?"
print(recent_date)
## [1] "1997-02-11 UTC"
#在此輸入你對本題的解讀
#定義一個新的數據框 #recent_purchase,該數據框將存儲篩選和排序後的結果,源自之前篩選的 top_customer_orders 數據框。
#使用 arrange 函數對 top_customer_orders 中的數據根據 OrderDate #進行降序排序(最新的日期在最前面)。這樣可以確保最近的消費在最上面。
#使用 slice 函數選擇排序後的第一行,這行代表顧客最近的一筆消費。
#從 recent_purchase 數據框中提取 OrderDate #欄位的值,這是顧客最近一次消費的日期,並將其存儲在變數 recent_date 中。
#輸出顧客最近一次消費的具體日期。