简体   繁体   中英

How can I paginate this BigQuery query using the rownumber?

How can this BigQuery query be updated to allow for the additional clause for pagination?

SELECT test.id, test.city, ROW_NUMBER() OVER () rownumber FROM prod.test LEFT OUTER JOIN prod.locations AS location ON (test.city = location.id)
WHERE active = true 
AND rownumber BETWEEN 10000 AND 30000 

The resulting error is:

Unrecognized name: rownumber

For this example, use a subquery:

SELECT t.*
FROM (SELECT test.id, test.city, ROW_NUMBER() OVER () rownumber
      FROM prod.test LEFT OUTER JOIN
           prod.locations location
           ON test.city = location.id
      WHERE active = true
     ) t 
WHERE rownumber BETWEEN 10000 AND 30000 ;

However, you should probably be using LIMIT and OFFSET .

you can also use CTE

with cte as
(
 SELECT test.id, test.city, ROW_NUMBER() OVER () rownumber 
 FROM prod.test LEFT OUTER JOIN prod.locations AS location ON (test.city = location.id)
WHERE active = true
 ) select * from cte where rownumber BETWEEN 10000 AND 30000 

rownumber is a inline alias which not supported in where clause directly

You can use this standard sql clause if bigquery supports it,

SELECT 
       test.id
      ,test.city
FROM prod.test as test 
LEFT OUTER JOIN prod.locations AS location ON (test.city = location.id)
    WHERE active = true
order by test.id
offset 10000 rows fetch next 20000 rows only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM