how to find number of occurences of a value in a field?

D

djc

I have a recordset with say 3 fields: FirstName, LastName, Address. I want
to run a query to determine which LastName appears the most amount of times
and how many times that was. AND another query to find the opposite: which
one appears the least amount of times and how many times that was.

can I do this with sql?

note: there would be far to many unique 'LastName' s to write a seperate
select query on each one and compare the count.

anyone?
 
K

Ken Snell

Here is sample SQL that will show the LastName with most occurrences:

SELECT TOP 1 Count(LastName) AS CountOfLastName, LastName
FROM TableName
GROUP BY LastName
ORDER BY Count(LastName) DESC;


Here is sample for the fewest occurrences:

SELECT TOP 1 Count(LastName) AS CountOfLastName, LastName
FROM TableName
GROUP BY LastName
ORDER BY Count(LastName);
 
D

djc

Sweet! you rule!

Ken Snell said:
Here is sample SQL that will show the LastName with most occurrences:

SELECT TOP 1 Count(LastName) AS CountOfLastName, LastName
FROM TableName
GROUP BY LastName
ORDER BY Count(LastName) DESC;


Here is sample for the fewest occurrences:

SELECT TOP 1 Count(LastName) AS CountOfLastName, LastName
FROM TableName
GROUP BY LastName
ORDER BY Count(LastName);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top