简体   繁体   English

大查询:动态替换多列中的字符串部分

[英]Big query: Replace string parts in multiple columns dynamically

I have a dynamic table with an indertermined number of columns [statement_nn] with strings.我有一个动态表,其中包含带字符串的列 [statement_nn] 的数量不确定。 I need to replace string parts by the correspondent item我需要用相应的项目替换字符串部分

The table looks like this: Original table该表如下所示:原始表

How could I do the operation having in mind that “statement” columns are variable and cannot specifically use them in a standard REPLACE statement?考虑到“语句”列是可变的并且不能在标准 REPLACE 语句中专门使用它们,我该如何进行操作?

This is the end result Im looking to get: Desired result这是我希望得到的最终结果: Desired result

I tried to make an array of "statement" columns and replace items there, but I need to be able to keep column names to get the desired result我试图制作一个“声明”列数组并在那里替换项目,但我需要能够保留列名以获得所需的结果

You can use EXECUTE IMMEDIATE to dynamically select and replace your desired items.您可以使用EXECUTE IMMEDIATE动态 select 并替换您想要的项目。 The way I solved it was replacing each placeholder manually with the following query:我解决它的方法是用以下查询手动替换每个占位符:

execute immediate (
select 'select *  replace(' || 
  string_agg('regexp_replace(' || 
    'regexp_replace(' || 
      'regexp_replace(' || column_name || ',' || ' r"<name>", name)' 
      || ',' || ' r"<surname>", surname)' 
      || ',' || ' r"<registration_year>", CAST(registration_year as STRING))' 
      || ' as ' || column_name, ', ') || 
') from project_name.dataset_name.table_name'
from `project_name.dataset_name.INFORMATION_SCHEMA.COLUMNS`
where table_name = 'table_name'
and STARTS_WITH(column_name, "statement_")
)

Remember to replace project_name , dataset_name , and table_name .请记住替换project_namedataset_nametable_name

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

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