BigQuery SQL如何在使用LIMIT时获得总计数

时间:2016-05-08 05:55:31

标签: sql google-bigquery

如果我在SQL查询中使用LIMIT 10(使用BigQuery),是否还有一种方法可以返回总计数?

例如,存在100行。如何查询返回前10个,但是如果不进行单独的count(id)聚合查询,还可以向用户显示总共有多少行?

2 个答案:

答案 0 :(得分:2)

要添加到Mikhail的答案,您可能希望这样做以查看分组查询中唯一值的计数。在以下示例中,R有10个唯一值,但您只想查看前4个,以及唯一行的计数。我还添加了显示每个组的计数和每行的总计数。 (下面的标准SQL)

WITH YourTable AS (
  SELECT 1 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 10 AS r    
)
SELECT 
  r,
  SUM(1) OVER (ORDER BY r ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CountOfAllUniqueRows,
  COUNT(r) AS CountOfEachR,
  SUM(COUNT(R)) OVER (ORDER BY r ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CountOfAllRows
FROM YourTable
GROUP BY r
ORDER BY r
LIMIT 4

并给出结果:

r   CountOfAllUniqueRows    CountOfEachR    CountOfAllRows
1   10                      8               68
2   10                      6               68
3   10                      7               68
4   10                      6               68

答案 1 :(得分:1)

不知道为什么你会这样做 - 可能是因为成本 - 所以你要避免第二次扫描 - 无论如何 - 下面"技巧'可能对你有用。
虽然只获得了你想要的那么多行 - 你也获得了总行数,但是在每个输出行中 - 所以你需要在向用户显示时自行处理

  

使用BigQuery Legacy SQL:

SELECT 
  r, cnt
FROM (
  SELECT 
    r,
    COUNT(r) OVER() AS cnt,
    ROW_NUMBER() OVER() AS line
  FROM 
    (SELECT 1 AS r),
    (SELECT 2 AS r),
    (SELECT 3 AS r),
    (SELECT 4 AS r),
    (SELECT 5 AS r),
    (SELECT 6 AS r),
    (SELECT 7 AS r),
    (SELECT 8 AS r),
    (SELECT 9 AS r),
    (SELECT 10 AS r)
)
WHERE line <= 4

SELECT 
  r,
  cnt
FROM (
  SELECT r
  FROM 
    (SELECT 1 AS r),
    (SELECT 2 AS r),
    (SELECT 3 AS r),
    (SELECT 4 AS r),
    (SELECT 5 AS r),
    (SELECT 6 AS r),
    (SELECT 7 AS r),
    (SELECT 8 AS r),
    (SELECT 9 AS r),
    (SELECT 10 AS r)
) AS YourTable
CROSS JOIN (
  SELECT COUNT(1) AS cnt
  FROM 
    (SELECT 1 AS r),
    (SELECT 2 AS r),
    (SELECT 3 AS r),
    (SELECT 4 AS r),
    (SELECT 5 AS r),
    (SELECT 6 AS r),
    (SELECT 7 AS r),
    (SELECT 8 AS r),
    (SELECT 9 AS r),
    (SELECT 10 AS r)
) rows
LIMIT 4 
  

使用BigQuery Standard SQL:

请勿忘记取消选中显示选项

下的Use Legacy SQL复选框
WITH YourTable AS (
  SELECT 1 AS r UNION ALL
  SELECT 2 AS r UNION ALL
  SELECT 3 AS r UNION ALL
  SELECT 4 AS r UNION ALL
  SELECT 5 AS r UNION ALL
  SELECT 6 AS r UNION ALL
  SELECT 7 AS r UNION ALL
  SELECT 8 AS r UNION ALL
  SELECT 9 AS r UNION ALL
  SELECT 10 AS r    
)
SELECT 
  r,
  (SELECT COUNT(1) FROM YourTable) AS cnt
FROM YourTable
LIMIT 4

在所有情况下结果都是

r   cnt  
1   10   
2   10   
3   10   
4   10