首页 > 解决方案 > 带条件的循环标识符

问题描述

我想以某种方式使标识符列存在,是否有可能在下面有类似的数据?逻辑:每行类型有相同的数据,标识符重复,直到有不同类型的数据,即使之前已经有相同类型的数据

dt          type    user_id  trx  identifier
2021-08-28  online  100      001  1
2021-08-29  online  100      002  1
2021-08-30  offline 100      003  2
2021-09-04  online  100      004  3
2021-09-08  web     100      005  4
2021-09-09  online  100      006  5
2021-09-11  offline 100      007  6
2021-09-15  offline 100      008  6

标签: sqlgoogle-bigquery

解决方案


考虑以下方法

select * except(new_type), 
  countif(new_type) over(partition by user_id order by trx) as identifier
from (
  select *, ifnull(type != lag(type) over(partition by user_id order by trx), true) as new_type
  from data 
)

如果应用于您问题中的样本数据 - 输出是

在此处输入图像描述


推荐阅读