Domanda

In BigQuery I have a query that uses the RANK() function to group users by total count across sources. I only care about the top 10 users. Currently, I am doing the RANK() function in a sub-select with the WHERE clause living in a parent query to limit the results. Could someone explain to me how I could accomplish this without the parent query? The query I'd like to run looks like this:

SELECT source, user, count( tweet_id ) as total, max( friends ) as friends, 
RANK() OVER (PARTITION BY source ORDER BY total DESC, friends DESC ) as user_rank
FROM tweets
GROUP BY source, user
HAVING user_rank <= 10

But I get Error: Field 'user_rank' not found in table 'tweets'. I can ORDER BY using user_rank, just not filter.

Thanks for any suggestions!

È stato utile?

Soluzione

You may have to do something like

SELECT source, user, total, friends, user_rank

FROM (
    SELECT source, user, count( tweet_id ) as total, max( friends ) as friends, 
    RANK() OVER (PARTITION BY source ORDER BY total DESC, friends DESC ) as user_rank
    FROM tweets
    GROUP BY source, user
) 

HAVING user_rank <= 10
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top