[英]How to create a new table that only keeps rows with more than 5 data records under the same id in Bigquery
I have a table like this:我有一张这样的桌子:
Id ![]() |
Date![]() |
Steps![]() |
Distance![]() |
---|---|---|---|
1 ![]() |
2016-06-01 ![]() |
1000 ![]() |
1 ![]() |
There are over 1000 records and 50 Ids in this table, most ids have about 20 records, and some ids only have 1, or 2 records which I think are useless.这个表有1000多条记录和50个id,大部分id有20条左右,有的id只有1、2条记录,我觉得没用。 I want to create a table that excludes those ids with less than 5 records.
我想创建一个表,排除那些少于 5 条记录的 ID。 I wrote this code to find the ids that I want to exclude:
我写了这段代码来查找我想排除的 ID:
SELECT
Id,
COUNT(Id) AS num_id
FROM `table`
GROUP BY
Id
ORDER BY
num_id
Since there are only two ids I need to exclude, I use WHERE clause:由于我只需要排除两个 ID,因此我使用 WHERE 子句:
CREATE TABLE `` AS
SELECT
*
FROM ``
WHERE
Id <> 2320127002
AND Id <> 7007744171
Although I can get the result I want, I think there are better ways to solve this kind of problem.虽然我可以得到我想要的结果,但我认为有更好的方法来解决这类问题。 For example, if there are over 20 ids with less than 5 records in this table, what shall I do?
比如这张表有20多个id少于5条记录怎么办? Thank you.
谢谢你。
Consider this:考虑一下:
CREATE TABLE `filtered_table` AS
SELECT *
FROM `table`
WHERE TRUE QUALIFY COUNT(*) OVER (PARTITION BY Id) >= 5
Note: You can remove WHERE TRUE
if it runs successfully without it.注意:如果在没有它的情况下成功运行,您可以删除
WHERE TRUE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.