20 Sep 19 · npack ·       Add to Favorites  

ROW_NUMBER() OVER PARTITON BY Syntax in Bigquery

Use the ROW_NUMBER() over analytic functions to Calculate ROW_NUMBER. Analytic functions are evaluated after aggregation (GROUP BY and non-analytic aggregate functions)

The following query finds the early employee in each department:

SELECT firstname, department, startdate,
ROW_NUMBER() OVER ( PARTITION BY department ORDER BY startdate ) AS rank
FROM Employees;

you can extend this query to only see the top users,

SELECT firstname, department, startdate,
ROW_NUMBER() OVER ( PARTITION BY department ORDER BY startdate ) AS rownum
FROM Employees where rownum=1;

npack

posted on 20 Sep 19

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds

Copied