Select max in nested queries

Asked

Viewed 62 times

3

I have to select the candidate who has more publications than any other candidate.

select candidato.nome, Count(1) as nr_publicacoes 
from candidato, cv,ficha_inscricao, publicacao_cv 
where candidato.bi = ficha_inscricao.bi and 
      candidato.bi = cv.bi and cv.cod_cv = publicacao_cv.cod_cv 
group by candidato.nome;

But when I do max, Orale SQL Developer does not give results or error messages. I don’t want to use the rownum, my goal is to solve using the max.

1 answer

1


In chat chat, @Danielamaia said that ROWNUM, only MAX, could not be used. So the original answer has been replaced by this now.

To avoid the use of ROWNUM, a CTE (Common Table Expression) can be used, as shown below:

with publicacao_cte (nome,nr_publicacoes)
AS
(
    select
        candidato.nome,
        Count(1) as nr_publicacoes
    from
        candidato,
        cv,
        ficha_inscricao,
        publicacao_cv
    where
        candidato.bi = ficha_inscricao.bi and
        candidato.bi = cv.bi and
        cv.cod_cv = publicacao_cv.cod_cv
    group by 
        candidato.nome
)

select nome
from publicacao_cte
where publicacao_cte.nr_publicacoes = (select max(nr_publicacoes) from publicacao_cte);
  • Solve, but the goal was to use understanding how to use the max instead of the rownum

  • 1

    I updated the question, see if that’s it, please.

  • In row 16 the identifier nr_publicacoes is invalid, there is no.

  • nr_publicacoes was a variable I was trying to create using the as

  • @Danielamaia, I don’t know what the structure of your database looks like. It’s certainly a mistake for my ignorance of your model. Field nr_publicacoes belongs to which table?

  • does not belong to any table, as you can see in my question, in the second row I am trying to create this new variable with the command as

  • Got it, this way you won’t solve this problem. You count the number of publications per candidate using the public table_cv?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.