【问题标题】:Filter out Duplicates that follow each other in Sequence with BigQuery SQL使用 BigQuery SQL 过滤掉按顺序排列的重复项
【发布时间】:2021-04-28 13:50:57
【问题描述】:

我正在寻找一种方法来过滤掉具有相同列值的行序列,同时保留序列的第一个元素。

给定下表:

id location timestamp
1 A 1001
2 B 1002
3 A 1003
4 A <--- 1004
5 A <--- 1005
6 B 1006

我想产生以下输出:

id location timestamp
1 A 1001
2 B 1002
3 A 1003
6 B 1006

目标是删除第 4 行和第 5 行,因为它们都在另一个位置 A 后面。只有第一次出现的序列应该保留。我正在使用 BigQuery SQL。

【问题讨论】:

  • timestamp 是唯一的吗?
  • @ATOMP 是的。

标签: sql google-bigquery


【解决方案1】:

只需使用lag():

select t.*
from (select t.*,
             lag(location) over (order by timestamp) as prev_location
      from t
     ) t
where prev_location is null or prev_location <> location;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2020-03-29
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多