0
select max(qtd) 
from (select count(int_re_usu) as qtd from tb_questionario_voar_resposta)

Why this query dont work? I want to retrieve the max value from all count

it says incorrect syntax near')'

Any ideias?

3
  • 4
    The query itself doesn't make sense. The subquery will always return one row (the total number of lines in your table), therefore selecting the maximum out of one row will always return this row... Commented Nov 16, 2010 at 16:25
  • 1
    I think you may be missing a GROUP BY clause in the sub-query. The query as written is the same as the sub-query "select count(int_re_usu) as qtd from tb_questionario_voar_resposta" Commented Nov 16, 2010 at 16:27
  • Like Vincent said - this doesn't make much sense - what are you trying to accomplish by using a sub query? Commented Nov 16, 2010 at 16:27

2 Answers 2

3

You need an alias on a derived table:

select max(qtd) 
from (
    select count(int_re_usu) as qtd 
    from tb_questionario_voar_resposta
) a

But as Vincent pointed out, that just returns one row, and I think you are missing a group by. You can really just do:

    select max(count(int_re_usu)) as qtd 
    from tb_questionario_voar_resposta
    group by SomeColumn
Sign up to request clarification or add additional context in comments.

2 Comments

Depending on the RDBMS, you may not be able to use nested aggregate functions. But anyway, selecting the max out of a count is useless (without GROUP BY).
no it says:Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
0

Try this you are missing a group by I think and use an alias-

     select 
              max(d1.qtd) 
        from 
        (
          select 
                count(int_re_usu) as qtd 
          from tb_questionario_voar_resposta
        group by qtd
        ) as d1
group by d1.qtd

2 Comments

You want to put the group by inside the subquery (or just remove it). But since OP didn't say anything about it, it's hard to know how to use it.
yea ur right..fixed it thanks.I am just trying to suggest it I am not sure what OP wants..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.