My target data/table:
mysql> select firstname from empl;
+-----------+
| firstname |
+-----------+
| Abhishek |
| Arnab |
| Aamaaan |
| Arbaaz |
| Mohon |
| Parikshit |
| Tom |
| Koustuv |
| Amit |
| Bibhishana|
| abCCdEeff |
+-----------+
11 rows in set (0.00 sec)
Desired output:
Return one row with three columns for each case-sensitive letter that repeats itself in a first name: column_one is x—the firstname in which a repeating letter is found; column_two is y—the leftmost-unique-letter that is repeated; column_three is z—the number of times the letter encountered in the word.
---------------+-------+-----+
firstname,x | str,y |cnt,z|
---------------+-------+-----+
Aamaaan | a | 4 |
Arbaaz | a | 2 |
Mohon | o | 2 |
Parikshit | i | 2 |
Koustuv | u | 2 |
Bhibhishana | h | 3 |
Bhibhishana | i | 2 |
Bhibhishana | a | 2 |
abcCCdEeff | C | 2 |
abcCCdEeff | f | 2 |
My best attempt, to-date:
WITH CTE AS
(
SELECT firstname, CONVERT(LEFT(firstname,1),CHAR) AS Letter, RIGHT(firstname, LENGTH(firstname)-1) AS Remainder
FROM empl
WHERE LENGTH(firstname)>1
UNION ALL
SELECT firstname, CONVERT(LEFT(Remainder,1),CHAR) AS Letter,
RIGHT(Remainder, LENGTH(Remainder)-1) AS Remainder
FROM CTE
WHERE LENGTH(Remainder)>0
)
SELECT firstname, Letter, ASCII(Letter) AS CharCode, COUNT(Letter) AS CountOfLetter
FROM CTE
GROUP BY firstname, Letter, ASCII(Letter)
HAVING COUNT(Letter)>2
Use recursive
CTE
s to get all the lettersA-Z
anda-z
and join to the table:See the demo.
Results: