April 24, 2003 at 9:45 am
Is there an easy way to return what the "average" or majority of a particular varchar field is? For example, say I have 6 records, if the field STATUS has the following per record "D, A, A, A, A, D" I want it to return "A" because it occurs most often. Can this be done easily?
April 24, 2003 at 9:56 am
Hi,
what about
SELECT TOP 1 <field_name>, COUNT(<some_field> AS tmp FROM <table_name>
GROUP BY <field_name> ORDER BY tmp DESC.
That'll only return the value which occurs most often. Leave out the TOP 1, you'll get a ranking.
HTH
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 24, 2003 at 10:57 am
Just correcting a typo in his SQL statement:
SELECT TOP 1 <field_name>, COUNT(<some_field>) AS tmp
FROM <table_name>
GROUP BY <field_name>
ORDER BY tmp DESC
April 24, 2003 at 11:03 am
oops, sorry if it has led to confusion
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 24, 2003 at 11:41 am
Thanks!! worked like a charm
Cheers to you too!
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply