문제

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!

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top