简体   繁体   中英

I want print word into each letter and count the letter how many times

sreenath i want ouput like this

select * from string_split(separator ' ');
s - 1
r - 1
e - 2
n - 1
t - 1

I understand that you are trying to generate a resultset that shows how many times each character appears in a word.

To achieve this, it would be good to set up a table that lists all characters whose occurrences you want to track.

CREATE TABLE alphabet ( 
    c varchar(1) NOT NUL PRIMARY KEY
);
INSERT INTO alphabet VALUES('a');
INSERT INTO alphabet VALUES('b');
...
INSERT INTO alphabet VALUES('z');

Now the following query should do the trick :

SELECT
    c.c,
    LENGTH(t.value)
        - LENGTH( REPLACE ( LCASE(t.value), c.c, '') ) occurrences 
FROM 
    mytable t
    INNER JOIN alphabet c
        ON INSTR(t.value, c.c) > 0

You can freely add any additional character in the alphabet (numeric maybe, or any special character). As per current query you should use lower case when feeding the alphabet table.

NB : this assumes that your data is stored in column "value" of table "mytable"

    -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM