简体   繁体   English

检查组中是否存在值并将其分配给 BigQuery 中的所有组行

[英]Check if value exists in group and assign it to all rows of group in BigQuery

I would like to check if the value a exists in each user id group and then assign this value to all rows of this user id/group.我想检查每个用户 ID 组中是否存在值a ,然后将此值分配给该用户 ID/组的所有行。 For example for the following data:例如对于以下数据:

user_id type    
    123    a
    123    b
    234    c

I should get:我应该得到:

user_id type    
    123    a
    123    a
    234    c

You can achieve this by executing a query similar to this:您可以通过执行与此类似的查询来实现此目的:

UPDATE so_test.a_updates
SET type = 'a'
where user_id in (
  select user_id
  from `elzagales.so_test.a_updates`
  where type = 'a'
);

additionally if this is something you want to repeatedly do you can make this a stored procedure using a similar structure.此外,如果这是您想要重复执行的操作,您可以使用类似的结构将其设为存储过程。

Consider below approach考虑以下方法

select *, 
  if(0 = countif(type = 'a') over(partition by user_id), type, 'a') as assigned_type,
from your_table       

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM