作业:
显示平均工资低于公司平均工资的部门中平均工资最高的部门名称。 [提示:使用嵌套子查询]
我的尝试:
Select Department, avg(Salary) as "Highest Average Salary"
from Employees
group by Department
having avg(Salary) > (
select Department, avg(Salary) as "Average Salary"
from Employees
group by Department
having avg(Salary) < (select avg(Salary) from Employees));
该错误是因为子查询返回多列,并且可能返回多行。当您使用子查询作为表达式时,它必须仅返回一列,且最多一行(如果它返回零行,则该值将被视为
NULL
)。您无法将 AVG(Salary)
与多个值进行比较。
子查询正确地找到了平均工资低于公司平均工资的部门。但是您在主查询中没有正确使用它。
您不应该在
WHERE
子句中使用它。您应该将其放在另一个子查询中以获得平均值的最大值(练习中的“平均工资是最高的”)。然后,您可以在主查询的 HAVING
子句中使用它来查找具有该平均工资的所有部门。
SELECT Department
FROM Employees
GROUP BY Department
HAVING AVG(Salary) = (
SELECT MAX(AvgSalary)
FROM (
select avg(Salary) as AvgSalary
from Employees
group by Department
having AvgSalary < (select avg(Salary) from Employees)
)
)
选择产品线,客户名称,totale,dense_rank()OVER(按产品线分区,客户名称按totale desc排序) from ( SELECT ProductLines.productline,customers.customerName, sum(quantityOrdered) 总计来自 客户内部加入订单上的customer.customerNumber =orders.customerNumber 在orders.orderNumber = orderdetails.orderNumber上加入orderdetails 在 products.productCode = orderdetails.productCode 上加入产品 将产品线加入到productlines.productLine= products.productLine GROUP by Productline 上,customerName order by sum(quantityOrdered) desc ) as lista GROUP by Productline ORDER BY
lista
.customerName
ASC;
现在你会在结果中找到回头客吗? 我不这么认为..