如何找到不同的名称(在第 1 列中),其权重(在第 2 列中)在 Big Query 中的几周内(在第 3 列中)总是增加?

how to find the distinct names (in column 1) whose weight (in column 2) always increased over the the weeks (in column 3) in Big Query?

我有一个很大的查询结果,它显示每个人在很多周内的体重,我想找到这些人的名字,这些人的体重在这几周内一直在增加。以下是示例数据。

    name        week        weight
    tom         2020W10     76
    tom         2020W09     75
    tom         2020W08     74
    jane        2020W10     65
    jane        2020W09     65
    jane        2020W08     64

所以对于上述样本数据,我只想在我的结果中看到汤姆,因为他的体重总是随着日期的推移而增加。在我的原始数据集中,我有 10,000 个名字和超过 10 周的时间,所以用更蛮力的方法真的很难做到。我正在考虑在大查询中使用回归函数,但是我必须做尽可能多的回归,因为我有唯一的名字。我真的很感激任何帮助。谢谢!

一种方法使用 lag() 和聚合:

with t AS (
      SELECT 'tom' AS name, '2020W10' AS week, 76 AS weight UNION ALL
      select 'tom' , '2020W09' , 75 UNION ALL
      select 'tom' , '2020W08' , 74 UNION ALL
      select 'jane' , '2020W10' , 65 UNION ALL
      select 'jane' , '2020W09' , 65 UNION ALL
      select 'jane' , '2020W08' , 64
    )
select t.name
from (select t.*, lag(weight) over (partition by name order by week) as prev_weight
      from t
     ) t
group by t.name
having countif(prev_weight >= weight) = 0 ;