How do I get max value based on count?

  • I have a table that has some result that scan over a period of several days. I want to display a value who's count is the largest over the time range. For example I have the following values in a column.

    EmpID Name Value

    1 Tim One

    1 Tim Two

    1 Tim Three

    1 Tim One

    1 Tim One

    1 Tim Two

    1 Tim One

    Since One shows up the most, I want to display that value. I would like my output to be

    EmpId Name Value

    1 Tim One

    The other scenario is when I have two counts that are the same. For example:

    EmpID Name Value

    1 Tim One

    1 Tim Two

    1 Tim Three

    1 Tim One

    1 Tim Two

    1 Tim Two

    1 Tim One

    In this case, I would want to select the maximum of value which would be Two in this case.

  • jkury (4/20/2013)


    I have a table that has some result that scan over a period of several days. I want to display a value who's count is the largest over the time range. For example I have the following values in a column.

    EmpID Name Value

    1 Tim One

    1 Tim Two

    1 Tim Three

    1 Tim One

    1 Tim One

    1 Tim Two

    1 Tim One

    Since One shows up the most, I want to display that value. I would like my output to be

    EmpId Name Value

    1 Tim One

    The other scenario is when I have two counts that are the same. For example:

    EmpID Name Value

    1 Tim One

    1 Tim Two

    1 Tim Three

    1 Tim One

    1 Tim Two

    1 Tim Two

    1 Tim One

    In this case, I would want to select the maximum of value which would be Two in this case.

    what have you tried so far..? .post the code that is causing your problem.

    hint...GROUP BY and COUNT

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • J Livingston SQL (4/20/2013)


    jkury (4/20/2013)


    I have a table that has some result that scan over a period of several days. I want to display a value who's count is the largest over the time range. For example I have the following values in a column.

    EmpID Name Value

    1 Tim One

    1 Tim Two

    1 Tim Three

    1 Tim One

    1 Tim One

    1 Tim Two

    1 Tim One

    Since One shows up the most, I want to display that value. I would like my output to be

    EmpId Name Value

    1 Tim One

    The other scenario is when I have two counts that are the same. For example:

    EmpID Name Value

    1 Tim One

    1 Tim Two

    1 Tim Three

    1 Tim One

    1 Tim Two

    1 Tim Two

    1 Tim One

    In this case, I would want to select the maximum of value which would be Two in this case.

    what have you tried so far..? .post the code that is causing your problem.

    hint...GROUP BY and COUNT

    I think I found my solution on another post.

    ;with cte as (

    select empId, name,ROW_NUMBER() over (Partition by empId, name order by count(value) desc) as ranks

    from table group by empId, name

    )

    select empId, name, value from cte where cte.ranks=1

    This is the first time I am using CTE.

  • You store the number as a text representation? Have you tested it with other values? I think you may have just got lucky here?

    Yes, Two is "higher" than One ("T" > "O") but what if the 2 highest (equal) records are Two and Three? 3 is higher than 2 but "Tw" is > Th"

    Dird


    Dird

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply