要查询某人本月的总业绩在全公司的名次,可以使用以下MySQL语句:
SELECT t1.name, t1.total_sales, COUNT(t2.total_sales) AS rank
FROM (
SELECT name, SUM(sales) AS total_sales
FROM sales_table
WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE())
GROUP BY name
) AS t1
JOIN (
SELECT name, SUM(sales) AS total_sales
FROM sales_table
WHERE MONTH(date) = MONTH(CURRENT_DATE()) AND YEAR(date) = YEAR(CURRENT_DATE())
GROUP BY name
) AS t2
ON t1.total_sales <= t2.total_sales
GROUP BY t1.name, t1.total_sales
ORDER BY t1.total_sales DESC;
请注意,上述查询语句假设你的销售数据存储在名为sales_table
的表中,该表包含以下列:
name
:销售人员姓名sales
:销售额date
:销售日期如果你的表结构不同,请根据实际情况进行调整。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站