第一步:按照题目要求,找出product_name在C++、Java、Python中的订单,并且筛选日期大于2025-10-15
SELECT * FROM order_info WHERE product_name IN ("C++","Java","Python") AND DATE> "2025-10-15"显示结果如图:
第二步:上窗口函数,统计user_id中status的数量
SELECT user_id, product_name, STATUS, DATE, COUNT(STATUS) over(PARTITION BY user_id ORDER BY STATUS) "cout" FROM (SELECT * FROM order_info WHERE product_name IN ("C++","Java","Python") AND DATE> "2025-10-15") t显示结果如图:
第三步:上图已经很明确了,根据题意,找出status为“completed”及“cout”大于等于2的数据即可,注意select后面加上distinct关键字
SELECT DISTINCT user_id FROM (SELECT user_id, product_name, STATUS, DATE, COUNT(STATUS) over(PARTITION BY user_id ORDER BY STATUS) "cout" FROM (SELECT * FROM order_info WHERE product_name IN ("C++","Java","Python") AND DATE> "2025-10-15")t ) tt WHERE STATUS = "completed" AND cout >= 2 ORDER BY user_id显示结果如图: