Skip to content
Advertisement

Combining individual select aggregate queries into a single query

I’m trying to optimise some SQL queries in a script of mine that uses a SQLite database. Currently I loop through creating multiple queries as below to get aggregate data for a list of IP addresses. Is there a way to run this as one query instead? Desired output is the “top 20 domains” for each IP.

Current results, with 1 query run per IP. I’d like to combine these into 1 query if possible.

Advertisement

Answer

You must group by ipaddr and domain and use ROW_NUMBER() (or RANK()) window function to rank the the domains:

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