Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can an get count of the unique lengths of a string in database rows?

Tags:

sql

oracle

I am using Oracle and I have a table with 1000 rows. There is a last name field and

I want to know the lengths of the name field but I don't want it for every row. I want a count of the various lengths.

Example:

lastname:

smith
smith
Johnson
Johnson
Jackson
Baggins

There are two smiths length of five. Four others, length of seven. I want my query to return

7
5

If there were 1,000 names I expect to get all kinds of lengths.

I tried,

Select count(*) as total, lastname from myNames group by total

It didn't know what total was. Grouping by lastname just groups on each individual name unless it's a different last name, which is as expected but not what I need.

Can this be done in one SQL query?

like image 780
johnny Avatar asked Nov 18 '11 15:11

johnny


1 Answers

SELECT Length(lastname)
FROM MyTable
GROUP BY Length(lastname)
like image 108
Joel Coehoorn Avatar answered Sep 20 '22 19:09

Joel Coehoorn