С рекурсивом CTE
:
with
recursive cte as (
select 1 pos, substr('This is an example', 1, 1) letter
union all
select pos + 1, substr('This is an example', pos + 1, 1)
from cte
where pos < length('This is an example')
),
letters as (select distinct letter from cte where letter <> ' ')
select
letter,
length('This is an example') - length(replace('This is an example', letter, '')) counter
from letters
или:
with
recursive cte as (
select 1 pos, substr('This is an example', 1, 1) letter
union all
select pos + 1, substr('This is an example', pos + 1, 1)
from cte
where pos < length('This is an example')
)
select
letter,
count(*) counter
from cte
where letter <> ' '
group by letter
Для случая столбца в таблице:
create table tablename(id int, col text);
insert into tablename(id, col) values
(1, 'This is an example'),
(2, 'Another one'),
(3, 'XY');
with
recursive cte as (
select id, col, 1 pos, substr(col, 1, 1) letter
from tablename
union all
select id, col, pos + 1, substr(col, pos + 1, 1)
from cte
where pos < length(col)
)
select
id, col,
letter,
count(*) counter
from cte
where letter <> ' '
group by id, letter;
См. демо .