牛客网题目:SQL81 牛客的课程订单分析(五)
在使用 MySQL 窗口函数时一直报错不得其解:
报错:
SQL_ERROR_INFO: "You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'rank,\n# row_number() over
(partition by user_id order by date) bd_r' at line 69"
直到第二天各种尝试才发现问题所在:
不可使用一些关键字作为别名
解决
将错误代码 ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY date) rank
改为:row_number() over (partition by user_id order by date) bd_rk
附源原代码:
SELECT *,
ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY date) rank, # 错误写法(不应该直接使用rank)
# row_number() over (partition by user_id order by date) bd_rk, # 正确写法(或者使用'rank')
COUNT(product_name) OVER(PARTITION BY user_id ORDER BY date) number
FROM order_info
WHERE date > '2025-10-15'
AND status = 'completed'
AND product_name IN ('C++', 'Java', 'Python')