-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to count the number of occurrences of an item in a a list across all rows? #4169
Comments
Howdy and welcome to SQL-Land! :-) Two functions will do the trick for you! String_split, and unnest. Unnest is a special function that takes lists and creates a separate row for each element. With lists_split_into_rows as (
select
col1,
unnest(string_split(col2, ' ')) as new_column
from my_table
)
Select
new_column,
count(*) as my_count
from lists_split_into_rows
Group by
New_column |
@Alex-Monahan This worked! Do you have an idea of how I can query this result from the Python API? |
Nevermind, got it.
|
If you are using Pandas, I recommend |
Thanks for the tip! I ended up storing the string as a I tried doing:
But it seems like that returns |
Howdy! I think the function you are looking for is |
This function is pretty close, but it joins with a comma (I cannot control the separator, which is important here). Edit: That's not the right function either. It's not a big deal though, I can just do the relevant processing in Python |
Ah, sorry! I'm not at a desktop to validate... select
id,
string_agg(unnest(tags),' ') as text
from items
group by
id |
Thanks for the solution! I haven't had the chance to try it yet, because I found a different workaround, but when I revisit the code, I'll see if it works. |
Hi! Not sure if this is the best place to ask questions, but I'd figured I'd give it a shot.
Let's say I have 2 rows (right now the second column is a string, but it could easily be a list):
And I want to write a query that outputs something like:
what might be a good query to do this / can someone provide a bit of guidance?
I've been investigating the
LIST
andMAP
datatypes, which seem useful for this. But I'm pretty unused to the declarative style of SQL.If this was JS I'd do:
but I have no idea how to make loops in SQL / no sure if that's even the thing I should be doing
The text was updated successfully, but these errors were encountered: