Skip to content
Advertisement

SQL query to split and keep only the top N values

I have the following table data:

I need to create a table with a split items column, but with the limitation to have at most N items per name. E.g. for N = 3 the table should look like this:


I have the following query that splits items correctly, but doesn’t account for the maximum number N. What should I modify in the query (standard SQL, BigQuery) to account for N?

Advertisement

Answer

Consider below approach (BigQuery)

if applied to sample data in your question – output is

enter image description here

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