Wednesday, March 28, 2012

Query Problem

Hi,
I'm having a problem with a query... Asume I have the one table with two
columns
Col1 Col2
Mark 3
Clara 5
Bob 7
Clara 5
Clara 8
What I whant is to get the value of the most repeated entry in Col1 (in this
case, Clara)... any ideas?
Thanks
Here's one way:
SELECT TOP 1 WITH TIES Col1
FROM Tbl
GROUP BY Col1
ORDER BY COUNT(*) DESC
... Note I added WITH TIES -- It is possible that there can be multiple
values with the same number of rows, so you need to decide how you want to
handle that situation.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
"Alejandro" <Alejandro@.discussions.microsoft.com> wrote in message
news:6749A743-6C80-4A1E-8393-113079F1F8C7@.microsoft.com...
> Hi,
> I'm having a problem with a query... Asume I have the one table with two
> columns
> Col1 Col2
> Mark 3
> Clara 5
> Bob 7
> Clara 5
> Clara 8
> What I whant is to get the value of the most repeated entry in Col1 (in
this
> case, Clara)... any ideas?
> Thanks
|||Thanks... It works fine...
"Alejandro" wrote:

> Hi,
> I'm having a problem with a query... Asume I have the one table with two
> columns
> Col1 Col2
> Mark 3
> Clara 5
> Bob 7
> Clara 5
> Clara 8
> What I whant is to get the value of the most repeated entry in Col1 (in this
> case, Clara)... any ideas?
> Thanks

No comments:

Post a Comment