sql >> Base de Datos >  >> RDS >> Mysql

mysql Cuenta múltiples ocurrencias de entradas multiplexadas

Prueba esta consulta

SELECT Reason,COUNT(Reason) FROM
(
SELECT
  id,
  SUBSTRING_INDEX(SUBSTRING_INDEX(reasoncode, ',', n.digit+1), ',', -1) Reason
FROM
  table1
  INNER JOIN
  (SELECT 0 digit UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) n
  ON LENGTH(REPLACE(reasoncode, ',' , '')) <= LENGTH(reasoncode)-n.digit
ORDER BY
  id,
  n.digit
) T

Group By Reason;

SQL FIDDLE

La salida sería:

REASON  OCCURANCES
0           3
1           3
2           2
3           1