Skip to content
Advertisement

Count string occurances within a list column – Snowflake/SQL

I have a table with a column that contains a list of strings like below:

EXAMPLE:

QUESTION:

I want to be able to get a count of the amount of times each unique string appears (strings are seperable within the strings column by commas) but only know how to do the following:

However the above method doesn’t work as it only counts the number of user ids that use a specific grouping of strings.

The ideal output using the example above would like this!

DESIRED OUTPUT:

Advertisement

Answer

Based on your description, here is my sample table:

I used SPLIT_TO_TABLE to split each string as a row, and then REGEXP_SUBSTR to clean the data. So here’s the query and output:

SPLIT_TO_TABLE https://docs.snowflake.com/en/sql-reference/functions/split_to_table.html REGEXP_SUBSTR https://docs.snowflake.com/en/sql-reference/functions/regexp_substr.html

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement