CASE WHEN BigQuery 中的大小写比较同一字段中的值

CASE WHEN case in BigQuery comparing value in the same field

我需要你在 BigQuery 方面的帮助。我正在尝试在同一字段中使用多个条件时执行 CASE。这是数据:

在这种情况下,我想要一个聚合输出,当 'Resurrected' movement_type 发生在 'Churned' movement_type 之后时,根据其 month_key(无论他们之间的差距)。

因此,根据该数据,我希望输出为:

+----------+-----------+
| group_id | condition |
+----------+-----------+
| A12345   | TRUE      |
+----------+-----------+

我试过使用 LEAD,但没能找到正确答案。谢谢。

wanna have one aggregated output that says TRUE when 'Resurrected' movement_type happens AFTER 'Churned' movement_type based on its month_key

只需使用聚合:

select group_id,
       (max(case when movement_type = 'Resurrected' then month_key end) >
        max(case when movement_type = 'Churned' then month_key end)
       ) as flag
from t
group by group_id;

考虑以下方法

select group_id, logical_or(flag) as condition 
from (
  select group_id, 
    countif(movement_type = 'Churned') over prev_rows > 0
    and movement_type = 'Resurrected' as flag
  from `project.dataset.table` 
  window prev_rows as (partition by group_id order by month_key)
)
group by group_id