gpt4 book ai didi

mysql - 查询以查找每年销售额超过平均销售额的销售人员列表

转载 作者:行者123 更新时间:2023-11-29 05:04:21 24 4
gpt4 key购买 nike

我有一张表(表的链接:https://docs.google.com/spreadsheets/d/11P_ElrtCXwE3NYAoP-auO7w-Et_zd6omn-v-zMdu8KA/edit?usp=sharing),我必须从中找到每年销售额超过平均销售额的销售人员名单。

我在下面写了一个查询,但它不起作用。

SELECT t1.salesman, t1.AVG(sale), t1.year
SUM(CASE WHEN t1.AVG(sale)>t2.AVG(sale) THEN 1 ELSE 0 END)>0
FROM Sales_temp as t1
LEFT JOIN
(SELECT t2.year, t2.AVG(sale)
FROM Sales_temp as t2
Group by t2.year)
ON t1.year = t2.year
Group by t1.salesman

任何帮助将不胜感激。

最佳答案

试试这个:

SELECT salesman, sale, year
FROM Sales_temp
INNER JOIN
(
SELECT year ayear, AVG(sale) asale
FROM Sales_temp
GROUP BY year
) atbl
ON year = ayear AND sale > asale
ORDER BY year, salesman;

通过给你的子查询列别名,你可以不用表的别名。这稍微简化了事情。我将您的 LEFT JOIN 更改为 INNER JOIN,因为这会将您的输出限制为那些可以连接的记录,即。 e.有 sale>asale

我还添加了 ORDER BY 子句以提高结果的可读性。

关于mysql - 查询以查找每年销售额超过平均销售额的销售人员列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com