简体   繁体   English

如何创建一个新表,只保留Bigquery中相同ID下超过5条数据记录的行

[英]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 ID Date日期 Steps脚步 Distance距离
1 1个 2016-06-01 2016-06-01 1000 1000 1 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.

相关问题 如何在bigquery中获取过期的表数据,如果过期时间超过两天? - How to get expired table data in bigquery, If the expired time is more than two days? 如何从 bigquery-public-data 在 bigquery 中创建表 - How to create table in the bigquery from the bigquery-public-data 每个 ID 在 BigQuery 表中随机 select 行 - Randomly select rows in BigQuery table for each ID 如何在 Google BigQuery 上创建嵌套表并保持相同级别的数据聚合 - How to create a nested table on Google BigQuery and maintaining the same level of data aggregation KQL:: 只返回超过 4 条记录的标签 - KQL :: return only tags with more than 4 records 如何在 BIgQuery 中创建表? - How to CREATE TABLE in BIgQuery? 如何删除 BigQuery 表中的重复记录? - How do you deduplicate records in a BigQuery table? 每天我都会在 BigQuery 中收到一个新表,我想将这个新表数据连接到主表,数据集架构相同 - Daily I’m receiving a new table in the BigQuery, I want concatenate this new table data to the main table, dataset schema are same Bigquery 连接 2 个表,id 从 4 列连接并动态创建一个新表 - Bigquery join 2 tables with id concated from 4 columns and create a new table dynamically 如何将超过 25 个项目/行写入 DynamoDB 表? - How to write more than 25 items/rows into Table for DynamoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM