Tuesday, July 3, 2012

Counting unique values in MySQL

One of my areas of interest is digital humanities and text mining. Today I was looking for a simple way to count each unique value in a MySQL table.

For example, given a table "Text" with a column "Word", and with the following values:

Word
_____
To
be
or
not
to
be

... could we produce the following?

Repeats/Count
____________
to/2
be/2
or/1
not/1

A quick search brought up a number of possible solutions, of which the easiest was this one:

SELECT COUNT(*) AS Repeats, Word
FROM Text
GROUP BY Repeats
ORDER BY Repeats DESC;

With large texts, you may wish to eliminate some of the results:

SELECT COUNT(*) AS Repeats, Word
FROM Text
GROUP BY Repeats
HAVING repetitions > 1
ORDER BY Repeats DESC;

Voila'! :-)

No comments:

Post a Comment