Frage

I have a table with names in a given column. I need to know how many names have 1 letter, how many have 2 letters, how many have 3 letters etc up to 20 letters, broken down by another column (year born).

The output needs to be something like this:

1900 1 3
1900 2 45
1900 3 453
1900 5 897
...
1900 20 2

1901 1 4
1902 2 23
..
1902 20 3

In other words for 1900 3 people have a name that is 1-char long. 45 people's names are 2 characters long. 453 people's names are 3 chars long etc.

I know it will involve length() and count but not sure how to construct it.

War es hilfreich?

Lösung

This is an aggregation query, but with a function for the aggregation:

select YearBorn, length(name) as len, count(*) as cnt
from table t
group by YearBorn, length(name)
order by YearBorn, length(name);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top