简体   繁体   English

如何在 Google BigQuery 的字符串列中提取所有(包括 int 和 float)数值?

[英]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_columnTable_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_1numerical_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.

相关问题 如何从大查询表中的字符串列中提取所有数值并将它们插入到新的数值列中? - How to extract all numerical values from a string column in a big query table and insert them in new numerical columns? Google BigQuery使用regexp_extract从列中提取字符串 - Google BigQuery extract string from column with regexp_extract 如何仅从字符串中提取字母数字字符? (SQL Google BigQuery) - How extract only alphanumeric characters from string? (SQL Google BigQuery) 如何在 BigQuery 中转置单个列而不指定列中的所有值? - How to transpose a single column in BigQuery without specifying all the values in the column? 将嵌套值提取为列 Google BigQuery? - Extract nested values as columns Google BigQuery? Google Bigquery:如何在同一列中划分2个值? - Google Bigquery: How do I divide 2 values in the same column? 如何计算 BigQuery 中数组列的所有值的平均值和中位数? - How to calculate average and median of all the values of an array column in BigQuery? 在SQL中,如何根据另一列中的字符串值对一列中的数值求和 - In SQL, how to sum numerical values in one column based on string values in another column Google BigQuery:如何检查Year字符串是否包含负值 - Google BigQuery: How to check if Year string contains negative values 从数据块的数字列中提取非数字值 - Extract non numerical values from a numeric column in databricks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM