I need to count how many hash tags (or other text) appearances I have in a table. A hash tag placed in a cell (column), but a cell may contains more than one hash tag. A cell contains up to one hash tag of each type.
Example inspired from that question but need to get differ result.
Lets say I have a table that similar to that:
+----+-----------------------+-------------+--+
| id | Items | timestamp1 | |
+----+-----------------------+-------------+--+
| 1 | #Car #Dog #Fish | 01/01/2018 | |
| 2 | #Dog | 25/01/2018 | |
| 3 | #Fish #Dog | 18/03/2019 | |
| 4 | #Car #Dog #Fish | 23/06/2019 | |
| 5 | #Bird | 17/10/2019 | |
+----+-----------------------+-------------+--+
And I need to count how many times each item (#Car, #Dog, etc..) appear.
I have tried this
SELECT (CASE WHEN items like '%#Dog%' THEN 'Dogs'
WHEN items like '%#Car%' THEN 'Cars'
WHEN items like '%#Fish%' THEN 'Fish'
WHEN items like '%#Bird%' THEN 'Birds'
END) as Item, count(*)
FROM observations
GROUP BY (CASE WHEN items like '%#Dog%' THEN 'Dogs'
WHEN items like '%#Car%' THEN 'Cars'
WHEN items like '%#Fish%' THEN 'Fish'
WHEN items like '%#Bird%' THEN 'Birds'
END);
But this will give the (undesired) result:
+-------+----------+
| Item | count(*) |
+-------+----------+
| Birds | 1 |
| Dogs | 4 |
+-------+----------+
Desired results:
+-------+----------+
| count | Item |
+-------+----------+
| 4 | #Dog |
| 2 | #Car |
| 3 | #Fish |
| 1 | #Bird |
+-------+----------+
Is any good way to achieve that?