#第一步:筛选条件
#第二步:窗口函数:统计订单购买次数,并且左连接客户端表
#第三步:①筛选2次以上的订单②case when做条件判断找出source
#第四步:窗口函数:统计source次数 cnt
SELECT * FROM order_info WHERE DATE > "2025-10-15" AND STATUS = "completed" AND product_name IN("C++","Java","Python")
#第二步:窗口函数:统计订单购买次数,并且左连接客户端表
SELECT t.user_id, t.is_group_buy, cl.name, COUNT(STATUS) over(PARTITION BY user_id ORDER BY STATUS) "order_cnt" FROM (SELECT * FROM order_info WHERE DATE > "2025-10-15" AND STATUS = "completed" AND product_name IN("C++","Java","Python")) t LEFT JOIN CLIENT cl ON t.client_id = cl.id
#第三步:①筛选2次以上的订单②case when做条件判断找出source
SELECT CASE is_group_buy WHEN "No" THEN NAME WHEN "Yes" THEN "GroupBuy" END "source" FROM (SELECT t.user_id, t.is_group_buy, cl.name, COUNT(STATUS) over(PARTITION BY user_id ORDER BY STATUS) "order_cnt" FROM (SELECT * FROM order_info WHERE DATE > "2025-10-15" AND STATUS = "completed" AND product_name IN("C++","Java","Python")) t LEFT JOIN CLIENT cl ON t.client_id = cl.id) tt WHERE order_cnt >= 2
#第四步:窗口函数:统计source次数 cnt
SELECT DISTINCT source, COUNT(source) over(PARTITION BY source ORDER BY source) "cnt" FROM (SELECT CASE is_group_buy WHEN "No" THEN NAME WHEN "Yes" THEN "GroupBuy" END "source" FROM (SELECT t.user_id, t.is_group_buy, cl.name, COUNT(STATUS) over(PARTITION BY user_id ORDER BY STATUS) "order_cnt" FROM (SELECT * FROM order_info WHERE DATE > "2025-10-15" AND STATUS = "completed" AND product_name IN("C++","Java","Python")) t LEFT JOIN CLIENT cl ON t.client_id = cl.id) tt WHERE order_cnt >= 2) ttt