April 20, 2013 at 2:08 pm
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.
April 20, 2013 at 2:29 pm
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
April 20, 2013 at 2:51 pm
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.
April 20, 2013 at 4:20 pm
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
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply