473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Easy way to Calculate Mode

Hello,

I was wondering if there was an easy way for me to calculate the mode
for a field of values in one query. I have one field of values say
Names and all I want it to return is one record of the most common
name in my Names field. I tried a count function subquery within a max
function but it returns an error that I cannot aggregate functions...
*shrugs* I am quite a newbie at this so please forgive my ignorance.
Any help is much appreciated! Thanks in advance!

-mp
Nov 13 '05 #1
5 13673
I think you are after something like this if I understand you correctly

SELECT TOP 1 YourTable.YourNameField, Count(YourTable.YourNameField) AS
CountOfYourNameField

FROM YourTable

GROUP BY YourTable.YourNameField;

"mp141" <mp**@141xm.com> wrote in message
news:e9**************************@posting.google.c om...
Hello,

I was wondering if there was an easy way for me to calculate the mode
for a field of values in one query. I have one field of values say
Names and all I want it to return is one record of the most common
name in my Names field. I tried a count function subquery within a max
function but it returns an error that I cannot aggregate functions...
*shrugs* I am quite a newbie at this so please forgive my ignorance.
Any help is much appreciated! Thanks in advance!

-mp

Nov 13 '05 #2
"mp141" <mp**@141xm.com> wrote in message
news:e9**************************@posting.google.c om...
Hello,

I was wondering if there was an easy way for me to calculate the mode
for a field of values in one query. I have one field of values say
Names and all I want it to return is one record of the most common
name in my Names field. I tried a count function subquery within a max
function but it returns an error that I cannot aggregate functions...
*shrugs* I am quite a newbie at this so please forgive my ignorance.
Any help is much appreciated! Thanks in advance!

-mp


for a table called 'names', having a column called 'name', (consider
changing that - it's a reserved word), you can do this
select top 1 [name], count(*) as freq
from names
group by [name]
order by count(*) desc
Nov 13 '05 #3

"mp141" <mp**@141xm.com> wrote in message
news:e9**************************@posting.google.c om...
Hello,

I was wondering if there was an easy way for me to calculate the mode
for a field of values in one query. I have one field of values say
Names and all I want it to return is one record of the most common
name in my Names field. I tried a count function subquery within a max
function but it returns an error that I cannot aggregate functions...
*shrugs* I am quite a newbie at this so please forgive my ignorance.
Any help is much appreciated! Thanks in advance!

-mp


What happens if two names, say Alan and Bob both appear 5 times? Which
would you select? The query below would select only Alan's record because
his name comes first alphabetically:

SELECT TOP 1
ConName, Count(*) AS Mode
FROM tblContacts
GROUP BY ConName
ORDER BY Count(*) DESC , ConName ASC

Nov 13 '05 #4
Thank you Phil but I don't think that solved my problem. It is giving me
some arbitrary value (at least it looks arbitrary to me). Maybe I should
elaborate: I have a field that looks like this

Answer
1
2
3
4
2
2
Would I would like the query to do is return the mode, in this case "2".
Sounds easy but it's got me completely frazzled!

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5
Does this fit the bill?

SELECT TOP 1 Questionaire.Answer
FROM Questionaire
GROUP BY Questionaire.QuestionNumber, Questionaire.Answer
ORDER BY Count(Questionaire.Answer) DESC;

"M Pan" <mp**@141xm.com> wrote in message
news:41**********************@news.newsgroups.ws.. .
Thank you Phil but I don't think that solved my problem. It is giving me
some arbitrary value (at least it looks arbitrary to me). Maybe I should
elaborate: I have a field that looks like this

Answer
1
2
3
4
2
2
Would I would like the query to do is return the mode, in this case "2".
Sounds easy but it's got me completely frazzled!

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: PatchFactory Support | last post by:
Description: Professional and easy-to-use patch building environment that can help you to create instant patch packages for software and file updating. Generated patch packages are small size...
4
by: Eric | last post by:
Hey Everyone.. I have a form that has approximately 7 text fields and 1 checkbox. Generally when this form is submitted(to itself BTW) it works fine, however, when the checkbox is only field...
14
by: delerious | last post by:
I need to determine an element's width and height in pixels (not including padding, border, and margin) in Javascript. The element will not have width or height styles specified. In Mozilla, I...
1
by: Sam | last post by:
I successfully created DataGrid with connected environment but how do I perform sum of TotalRBAmount in DataGrid? Coding of ASP.Net 1.1 ------------------------ <form runat="server">...
3
by: Spooner | last post by:
Hi there, I don't know if the answer to this has been posted before, but I'm having some problem calculating the Mode for a dynamic array in C++. > cout << "Enter the size of your array: "; cin...
3
by: Carles Company Soler | last post by:
Hello, I want to calculate the value of an attribute. For example <rect x="2+3" y="12"and be <rect x="5" y="12">. Is it possible using XSLT? Thanks!
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
8
by: psy_berpunk | last post by:
hey, i'm trying to write a simple program to read gif87a non- interlaced format with a single image-descriptor --- I am using djgpp on windows xp. Sounds simple enough, unfortunatly data in...
6
by: LaundroMat | last post by:
Hi - I'm trying to calculate unique hash values for binary files, independent of their location and filename, and I was wondering whether I'm going in the right direction. Basically, the hash...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.