题解 | #牛客的课程订单分析(五)#

牛客的课程订单分析(五)

http://www.nowcoder.com/practice/348afda488554ceb922efd2f3effc427

SELECT
a.user_id,
b.first_buy_date,
c.second_buy_date,
a.cnt
FROM
(
SELECT
user_id,COUNT(1) AS cnt
FROM
order_info 
WHERE
date > '2025-10-15'
AND
status='completed'
AND
product_name IN('C++','Java','Python')
GROUP BY
user_id
HAVING
COUNT(1) >= 2
)a,
(
SELECT
min(date) AS first_buy_date,
user_id
FROM
order_info
WHERE
date > '2025-10-15'
AND
status='completed'
AND
product_name IN('C++','Java','Python')
GROUP BY
user_id
)b,
-- 在上面条件下第二天购买
(
SELECT a.user_id,MIN(a.date) AS second_buy_date
FROM
(
SELECT
user_id,date
FROM
order_info
WHERE
date > '2025-10-15'
AND
status='completed'
AND
product_name IN('C++','Java','Python')
)a
WHERE
NOT EXISTS (SELECT * FROM(
SELECT
user_id,min(date) AS first_buy_date
FROM
order_info
WHERE
date > '2025-10-15'
AND
status='completed'
AND
product_name IN('C++','Java','Python')
GROUP BY
user_id
)b WHERE a.user_id = b.user_id AND a.date = b.first_buy_date)
GROUP BY
a.user_id
)c
WHERE
a.user_id = b.user_id
AND
a.user_id = c.user_id
ORDER BY
a.user_id
全部评论

相关推荐

贺兰星辰:不要漏个人信息,除了简历模板不太好以外你这个个人简介是不是太夸大了...
点赞 评论 收藏
分享
喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务