![](/img/trans.png)
[英]How do I extract nested values from a JSON string in SQL on BigQuery?
[英]How to return difference in string values from the same column by doing a grouped string comparison in bigquery sql?
我有一个产品表,其中包含很多产品,例如:
产品 | 牌 |
---|---|
高露洁微笑 250gr | 高露洁 |
高露洁清新口气 250gr | 高露洁 |
高露洁薄荷 250gr | 高露洁 |
relx pod pro 芒果 - 1pod | 放松 |
relx pod pro 荔枝 - 1pod | 放松 |
烧酒真露 chamisul 绿葡萄 360ml | 真露 |
烧酒真露 chamisul 草莓 360ml | 真露 |
烧酒真露 chamisul 苹果葡萄 360ml | 真露 |
进入
产品 | 牌 | 单词 |
---|---|---|
高露洁微笑 250gr | 高露洁 | 微笑 |
高露洁清新口气 250gr | 高露洁 | 清新口气 |
高露洁薄荷 250gr | 高露洁 | 薄荷 |
relx pod pro 芒果 - 1pod | 放松 | 芒果 |
relx pod pro 荔枝 - 1pod | 放松 | 荔枝 |
烧酒真露 chamisul 绿葡萄 360ml | 真露 | 绿葡萄 |
烧酒真露 chamisul 草莓 360ml | 真露 | 草莓 |
烧酒真露 chamisul 苹果 360ml | 真露 | 苹果 |
我想按品牌分组并获得字符串的差异并将其作为新列返回。 我该如何进行转型? 并检查 regexp_contains(str_1, str_2_split)=false 并返回值?
考虑以下幼稚的方法
因此,查询如下所示
with common_words as (
select brand,
r'' || array_to_string(array(
select word
from t.words word
group by word
having count(*) = cnt
), '|') words
from (
select brand, count(*) cnt, array_concat_agg(words) words
from (
select brand, array(
select distinct word
from unnest(split(product, ' ')) word
) words
from your_table
)
group by brand
) t
)
select product, brand,
regexp_replace(trim(regexp_replace(product, words, '')), r'\s+', ' ') as diff
from your_table
join common_words
using (brand)
如果应用于您问题中的示例数据 - output 是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.