如何纠正元组排序中的错误? [URI 2992 最高平均工资部门]
Posted
技术标签:
【中文标题】如何纠正元组排序中的错误? [URI 2992 最高平均工资部门]【英文标题】:How can I correct the error in tuple ordering? [URI 2992 Highest Avarage Salary Divisions] 【发布时间】:2021-02-28 05:38:28 【问题描述】:谁能帮我理解错误?我无法正确排序。错误 25% URI。我的查询(GMB):
select name_dep, name_div, max(avg_salary)
from(
select departamento.nome as name_dep, divisao.nome as name_div, COALESCE(ROUND(AVG(vencimento.valor), 2) , 0) as avg_salary
from departamento, divisao, empregado, vencimento, emp_venc
where departamento.cod_dep=divisao.cod_dep and empregado.lotacao_div=divisao.cod_divisao and empregado.matr=emp_venc.matr and vencimento.cod_venc=emp_venc.cod_venc
group by departamento.nome, divisao.nome
) as foo
group by name_dep, name_div, avg_salary
order by avg_salary desc;
SQL:https://pastebin.com/7PyF9bDT
URI:https://www.urionlinejudge.com.br/judge/en/problems/view/2992
【问题讨论】:
【参考方案1】:您希望每个部门的薪水最高的部门。我会推荐distinct on
:
select distinct on (de.nome)
de.nome as name_dep,
di.nome as name_div,
coalesce(round(avg(ve.valor), 2) , 0) as avg_salary
from departamento de
inner join divisao di on di.cod_dep = de.cod_dep
inner join empregado em on em.lotacao_div = di.cod_divisao
inner join emp_venc ev on ev.matr = em.matr
inner join vencimento ve on ve.cod_venc = ev.cod_venc
group by de.nome, di.nome
order by de.nome, avg(ve.valor) desc
请注意,这使用标准的显式连接,而不是老式的隐式连接; (字面上)几十年前的这种语法不应该在新代码中使用。我还使用了表别名,这使查询更短且更具可读性。
如果你想允许顶部关系,那么另一种方法是使用窗口函数:
select *
from (
select
de.nome as name_dep,
di.nome as name_div,
coalesce(round(avg(ve.valor), 2) , 0) as avg_salary,
rank() over(partition by de.nome order by avg(ve.valor) desc) rn
from departamento de
inner join divisao di on di.cod_dep = de.cod_dep
inner join empregado em on em.lotacao_div = di.cod_divisao
inner join emp_venc ev on ev.matr = em.matr
inner join vencimento ve on ve.cod_venc = ev.cod_venc
group by de.nome, di.nome
) t
where rn = 1
order by name_dep
【讨论】:
以上是关于如何纠正元组排序中的错误? [URI 2992 最高平均工资部门]的主要内容,如果未能解决你的问题,请参考以下文章
如何修复“AADSTS90102:‘redirect_uri’值必须是有效的绝对 Uri。” Microsoft Graph 中的错误
SOAP:解组错误:意外元素(uri:“”,本地:“lastCallDate”)。预期元素是 <gracePeriod>、<token>