SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I get max value based on count?


How do I get max value based on count?

Author
Message
jkury
jkury
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 21
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.
J Livingston SQL
J Livingston SQL
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5435 Visits: 35344
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

jkury
jkury
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 21
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.
Dird
Dird
SSC-Addicted
SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)SSC-Addicted (467 reputation)

Group: General Forum Members
Points: 467 Visits: 808
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search