[英]How to extract all (including int and float) numerical values in a string column in Google BigQuery?
I have a table Table_1
on Google BigQuery which includes a string column str_column
.我在 Google BigQuery 上有一个表
Table_1
,其中包含一个字符串列str_column
。 I would like to write a SQL query (compatible with Google BigQuery) to extract all numerical values in str_column
and append them as new numerical columns to Table_1
.我想编写一个 SQL 查询(与 Google BigQuery 兼容)来提取
str_column
和Table_1
中的所有数值作为 Table_1 的新数值列。 For example, if str_column
includes first measurement is 22 and the other is 2.5
;例如,如果
str_column
包括first measurement is 22 and the other is 2.5
; I need to extract 22 and 2.5 and save them under new columns numerical_val_1
and numerical_val_2
.我需要提取 22 和 2.5 并将它们保存在新列
numerical_val_1
和numerical_val_2
下。 The number of new numerical columns should ideally be equal to the maximum number of numerical values in str_column
, but if that'd be too complex, extracting the first 2 numerical values in str_column
(and therefore 2 new columns) would be fine too.理想情况下,新数值列的数量应等于
str_column
中数值的最大数量,但如果这太复杂,则提取str_column
中的前 2 个数值(因此提取 2 个新列)也可以。 Any ideas?有任何想法吗?
Consider below approach考虑以下方法
select * from (
select str_column, offset + 1 as offset, num
from your_table, unnest(regexp_extract_all(str_column, r'\b([\d.]+)\b')) num with offset
)
pivot (min(num) as numerical_val for offset in (1,2,3))
if applied to sample data like in your question - output is如果应用于您的问题中的示例数据 - output 是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.