简体   繁体   English

如何使用行号对这个BigQuery查询进行分页?

[英]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? 如何更新此BigQuery查询以允许其他分页语句?

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 . 但是,您可能应该使用LIMITOFFSET

you can also use CTE 您也可以使用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 rownumber是直接在where子句中不支持的内联别名

You can use this standard sql clause if bigquery supports it, 如果bigquery支持,则可以使用此标准sql子句,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有RowNumber()的查询转换为LINQ? - How can I convert my query featuring RowNumber() to LINQ? 如何在同一查询中使用 rowNumber 获取 MAX rownumber - How to get MAX rownumber with rowNumber in same query 如何有效地对复杂 SQL 查询的结果进行分页? - How can I efficiently paginate the results of a complex SQL query? 如何减少 BigQuery 在查询期间扫描的数据量? - How can I reduce the amount of data scanned by BigQuery during a query? 如何在 BigQuery SQL 中使用 UNNEST 和 SPLIT 避免重复? - How can I avoid duplicates using UNNEST and SPLIT in BigQuery SQL? 如何在bigquery中使用正则表达式拆分字符串 - How can I split string using regular expression in bigquery 如何使用BigQuery查找连续的事件组? - How can I find continuously events groups using BigQuery? 如何在BigQuery的SQL查询中将这些行链接在一起? (我认为这需要CTE递归,BigQuery似乎不喜欢它。) - How can I link these rows together in an SQL query in BigQuery? (I think this requires CTE recursion, which BigQuery doesn't seem to like…) 如何编写查询以将行号(1到n)附加到每个组的每个记录 - How to write a query to attach rownumber(1 to n) to each records for each group 我可以使用RowNumber对我的结果集进行分类吗? - Can I use RowNumber to categorise my result set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM