Is it possible to count occurences of strings like ‘pass’ and ‘fail’ inside a Group_Concat()?
I have the following situation there I need to count the occurences of ‘pass’ and ‘fail’
I need to count how often ‘pass’ and ‘fail’ occurs in
GROUP_CONCAT(parameters.calc_value0 ORDER BY samples.id ASC SEPARATOR 'n')
Here is my current situation so far
SELECT parametertypes.name as 'Test',
CASE
WHEN (IFNULL(GROUP_CONCAT(parameters.calc_value0 ORDER BY samples.id ASC SEPARATOR 'n'), (GROUP_CONCAT(parameters.value0 ORDER BY samples.id ASC SEPARATOR 'n'))) LIKE '%pass%')
THEN CONCAT('<span class="green">', if(IFNULL(parameters.calc_value0, parameters.value0) LIKE '%pass%', COUNT(samples.id), COUNT(samples.id)) , ' pass </span>', CHAR(10), GROUP_CONCAT(DISTINCT CONCAT(samples.DDF_Artikelbezeichnung) ORDER BY samples.name ASC SEPARATOR 'n'))
ELSE '-'
END as 'Samples Pass',
CASE
WHEN (IFNULL(GROUP_CONCAT(parameters.calc_value0 ORDER BY samples.id ASC SEPARATOR 'n'), (GROUP_CONCAT(parameters.value0 ORDER BY samples.id ASC SEPARATOR 'n'))) LIKE '%fail%')
THEN CONCAT('<span class="red">', if(IFNULL(parameters.calc_value0, parameters.value0) LIKE '%fail%', COUNT(samples.id), COUNT(samples.id)), ' fail </span>', CHAR(10), GROUP_CONCAT(DISTINCT CONCAT(samples.DDF_Artikelbezeichnung) ORDER BY samples.name ASC SEPARATOR 'n'))
ELSE '-'
END as 'Samples Fail',
CASE
WHEN (IFNULL(GROUP_CONCAT(parameters.calc_value0 ORDER BY samples.id ASC SEPARATOR 'n'), (GROUP_CONCAT(parameters.value0 ORDER BY samples.id ASC SEPARATOR 'n'))) LIKE '%documentation%')
THEN CONCAT('<span class="orange">', if(IFNULL(parameters.calc_value0, parameters.value0) LIKE '%documentation%', COUNT(samples.id), COUNT(samples.id)), ' documentation </span>',CHAR(10), samples.DDF_Artikelbezeichnung)
ELSE '-'
END as 'Samples Documentation'
FROM samples
JOIN parameters ON parameters.sample_id = samples.id
JOIN parametertypes ON parametertypes.id = parameters.parametertype_id
WHERE samples.order_id IN(1268)
AND samples.visible_to != 'deleted'
AND parameters.calc_value0 IS NOT NULL
GROUP BY parametertypes.id -- , samples.DDF_Artikelbezeichnung
This are my current results. There should be 9 pass and 1 fail for first row.
2
Answers
When you need to count the amount of definite substring occurences in some string value then use the next pattern:
where
@string
is a value/column/expression to be investigated, and@substring
is a substring value/column/expression to be searched for.This method does not count the overlapped substrings – for example, counting for
'abab'
inxxabababyy
will produce 1 and not 2.If you need to count for more than one substring then count for each substring separately.
Here, in your case, we can use conditional aggregation to count the occurrences of
pass
andfail
. Use the following query please: