• 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.