473,806 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ 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 13721
I think you are after something like this if I understand you correctly

SELECT TOP 1 YourTable.YourN ameField, Count(YourTable .YourNameField) AS
CountOfYourName Field

FROM YourTable

GROUP BY YourTable.YourN ameField;

"mp141" <mp**@141xm.com > wrote in message
news:e9******** *************** ***@posting.goo gle.com...
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.goo gle.com...
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.goo gle.com...
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.An swer
FROM Questionaire
GROUP BY Questionaire.Qu estionNumber, Questionaire.An swer
ORDER BY Count(Questiona ire.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
1744
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 self-extracting executable update programs in a famous installer style with adjustable user-friendly interface and multilingual support. Enhanced with features like easy-to-use interface including a Wizard mode, powerful patch engine, integrated...
4
4302
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 that has been modified/clicked the form doesn't always submit. When it does work, a Stored procedure is passed form variables and updates to the db are made. When it doesn't, its as if the form wasn't submitted, it reloads and resets the page, but...
14
21549
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 know I can use document.defaultView.getComputedStyle() to do this. IE does not support document.defaultView.getComputedStyle(). It supports offsetWidth/offsetHeight, but those include the padding and border. IE also supports...
1
1532
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"> <wmx:SqlDataSourceControl id="SqlDataSourceControl1" runat="server" UpdateCommand="" SelectCommand="SELECT * FROM order
3
14069
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 >> aSize; int *newArray = new int; runMode(newArray, aSize); < this isi in my main -------------------------------------- void runMode( int intArray, int arraySize )
3
1751
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
11207
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 objects that are more than 4GB long. The problem is, when you have in thousands of places
8
1533
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 the format is arranged primarily in single-byte unsigned integers. So i've been reading them in a chars and casting them as unsigned chars into an int.
6
11383
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 values are calculated thusly: f = open('binaryfile.bin') import hashlib
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.