title: MySQL query to get count of repeating characters from a string
body: <h2>My target data/table:</h2>
  
  <pre><code>mysql&gt; select firstname from empl;
  +-----------+
  | firstname |
  +-----------+
  | Abhishek  |
  | Arnab     |
  | Aamaaan   |
  | Arbaaz    |
  | Mohon     |
  | Parikshit |
  | Tom       |
  | Koustuv   |
  | Amit      |
  | Bibhishana|
  | abCCdEeff |
  +-----------+
  11 rows in set (0.00 sec)
  </code></pre>
  
  <h2>Desired output:</h2>
  
  <p>Return one row with three columns for each <strong><em>case-sensitive</em></strong> letter that repeats itself in a first name: column_one is x—<strong>the firstname in which a repeating letter is found</strong>; column_two is y—<strong>the leftmost-unique-letter that is repeated</strong>; column_three is z—<strong><em>the number of times the letter encountered in the word</em></strong>.</p>
  
  <pre><code>---------------+-------+-----+
   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   |
  </code></pre>
  
  <p><strong>My best attempt, to-date:</strong></p>
  
  <pre><code>WITH CTE AS
  (
      SELECT firstname, CONVERT(LEFT(firstname,1),CHAR) AS Letter, RIGHT(firstname, LENGTH(firstname)-1) AS Remainder
      FROM empl
      WHERE LENGTH(firstname)&gt;1
      UNION ALL
      SELECT firstname, CONVERT(LEFT(Remainder,1),CHAR) AS Letter,
          RIGHT(Remainder, LENGTH(Remainder)-1) AS Remainder
      FROM CTE
      WHERE LENGTH(Remainder)&gt;0
  )
  SELECT firstname, Letter, ASCII(Letter) AS CharCode, COUNT(Letter) AS CountOfLetter
  FROM CTE
  GROUP BY firstname, Letter, ASCII(Letter)
  HAVING COUNT(Letter)&gt;2
  </code></pre>