SQL 新手,ROW_NUMBER()
如果查询已经包含GROUP BY
.
我举个例子:
我正在尝试为achievements
销售额最高的员工更新一个字段。Inachievements
应该出现 - Employee TOP-1 或 Employee TOP-2,具体取决于顶部的位置。
SELECT order_tab.employee_id,
SUM(order_item.price * order_item.quantity) AS total_sum,
COUNT(order_tab.order_id) AS total_clients,
row_number() over (PARTITION BY employee_id) AS number
FROM order_tab
JOIN order_item ON order_tab.order_id = order_item.order_id
GROUP BY order_tab.employee_id
ORDER BY total_sum DESC, total_clients DESC
LIMIT 3;
UPDATE employee SET achievements = concat_ws(', ', achievements, 'Работник ТОП-', number, ' за месяц')
WHERE employee.employee_id IN ( SELECT employee_id
FROM best_employees);
但首先,UPDATE 看不到请求中的数字字段。其次, row_number 不显示您需要的内容
你需要这样的:
那只是一个序列号