I am writing a basic SQL query to build a table in a cloud-based reporting tool, which supports a wide range of SQL "dialects" (no server-side access to the database). I have a working knowledge of SQL and it's needed to be able to construct a basic table for specific visualizations. Specifically, I need to visualize/quantify each unique instance of a value in a comma separated list from the original table. For my example, Col4 has the values a, b, c, d in various orders, which need to be counted and visualized to show how many times each value shows up each date (Col1).
The original table looks like this:
I'm using a CASE statement so I can manipulate the labeling of the output of Col4 in the new table so its easier to visualize.
Here's a shortened example of what my query looks like:
SELECT
Col1, CoL2,
CASE
WHEN Col4 LIKE '%a'
THEN 'custom label for a'
WHEN Col4 LIKE '%b%'
THEN 'Custom Label for b'
END AS 'Column Label'
FROM
Original_Table
The desired table (for the visualization to work should look like this):
That way, the visual will show each time "a" and "b" show up regardless of their position in the comma-separated list in Col4 in the original table (so I can show number of occurrences of "a" and "b" per month)
The problem is, this CASE statement only returns TRUE if one of the first WHEN conditions is met. For example, I'm seeing results wherein if a cell contains both "a" and "b", it will return only the first TRUE condition that's encountered and doesn't also return "b" from that same comma separated list. Conversely, if "b" is listed before "a" (the ordering is variable since the selections are coming from picklists populated by end-users), then "b" is returned "TRUE" and "a" is ignored.
Essentially, how can I tweak this query to return regardless of position and regardless if two values are in the same comma-separated list? Hope that makes sense and thanks!