0

how do i improved this code.

select code,max(total) from
(select code,count(*) from table_1 group by code) 

The above code,not working because I try to do MAX function onto the result set from query,but failed.

2

1 Answer 1

2

If you only want the number, then you can use this:

select max(total) 
from (
     select code,
            count(*) as total -- you forgot the column alias here
     from table_1 
     group by code
) 

If you want the code and the number, use the following:

with count_result as (
     select code,
            count(*) as total
     from table_1 
     group by code
) 
select code, 
       total
from count_result
where total = (select max(total) from count_result);
Sign up to request clarification or add additional context in comments.

Comments

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.