简体   繁体   English

如何基于 BigQuery 中的字符串模式 select 列

[英]How do I select columns based on a string pattern in BigQuery

I have a table in BigQuery with hundreds of columns, and it just happens that I want to select all of them except for those that begin with an underscore.我在 BigQuery 中有一个包含数百列的表,碰巧我想要 select 所有这些列,但以下划线开头的除外。 I know how to do a query to select the columns beginning with an underscore using the INFORAMTION_SCHEMA.COLUMNS table, but I can't figure out how I would use this query to select the columns I want.我知道如何使用INFORAMTION_SCHEMA.COLUMNS表对 select 以下划线开头的列进行查询,但我不知道如何使用此查询来查询我想要的列 select。 I know BigQuery has EXCEPT but I want to avoid writing out each column that begins with an underscore, and I can't seem to pass to it a subquery or even something like a._* .我知道 BigQuery 有EXCEPT但我想避免写出以下划线开头的每一列,而且我似乎无法将子查询甚至类似a._*东西传递给它。

Consider below approach考虑以下方法

execute immediate (select '''
   select * except(''' || string_agg(col) || ''') from your_table
'''
from (
  select col
  from (select * from your_table limit 1) t,
  unnest([struct(translate(to_json_string(t), '{}"', '') as kvs)]),
  unnest(split(kvs)) kv, 
  unnest([struct(split(kv, ':')[offset(0)] as col)])
  where starts_with(col, '_')
));           

if apply to table like below如果适用于下表

在此处输入图像描述

it generates below statement它生成以下语句

select * except(_c,_e) from your_table

and produces below output并产生以下 output

在此处输入图像描述

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

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