如何在SQL中获得“第n个最佳”记录

时间:2019-10-02 14:28:00

标签: sql google-bigquery

我知道如何查询前10条记录:

   // [] is passed as 2 args so that this effect will run once only on mount and unmount.

  useEffect(()=> {
    const unsubscribe = firebase.auth().onAuthStateChanged(function (_user) {
      if (_user) {
        // User is signed in.
        // do some firestroe queryes to get all the user's data
        setUser(_user);
      } else {
        setUser({ exists: false });
      }
    });

    // this method gets executed on component unmount.
    return () => {
      unsubscribe();
    }
  }, []);

但是,如果我想要第二好(或n最好)的10,该怎么办?我想我可以做

SELECT name, score FROM mytable ORDER BY score DESC LIMIT 10

但这似乎不必要地效率低下。是否有更惯用的返回方式,例如第11至20行? (我对Pandas更为熟悉,我会在其中使用索引。)

用例是我想从表中间导出数据,但是太大了,无法从顶部开始下载所有数据

1 个答案:

答案 0 :(得分:3)

ORDER BY也采用了OFFSET子句。这是您可以尝试的示例:

WITH T AS (
  -- Returns x values of 1 to 100
  SELECT x
  FROM UNNEST(GENERATE_ARRAY(1, 100)) AS x
)
SELECT x
FROM T
ORDER BY x LIMIT 10 OFFSET 10;

这将根据排序顺序返回第11至20行。您可以尝试更改限制或偏移量以查看不同的结果。