繁体   English   中英

如何从大查询表中的字符串列中提取所有数值并将它们插入到新的数值列中?

[英]How to extract all numerical values from a string column in a big query table and insert them in new numerical columns?

假设我有一张像temp_table这样的表:

CREATE TABLE `YOUR_DATASET.temp_table` (
  `F1` STRING,
  `F2` INT64,
  `F3` STRING,
);

此表包含一些数据:

INSERT `YOUR_DATASET.temp_table` (F1, F2, F3)
VALUES('45FG67', 10, 'This stri98ng includes 10/15 numbers .9'),
      ('45FG67', 10, 'This string includes 100 and 0'),
      ('95pp7', 30, 'This string includes .8 and 1_number'),
      ('45FG67', 12, '45'),
      ('45FG67', 12,NULL),
      ('95pp7', 30, NULL),
      ('95pp7', 5, '10 & 54.2')

这会将temp_table创建为:

SELECT * FROM `YOUR_DATASET.shc_core_2021.temp_table`

我想编写一个大查询脚本来提取F3和 append 中的所有数值作为temp_table的新数值列。 新数值列的数量应等于F3中数值的最大数量。 在这个示例表temp_table中,应该有 4 个新的数值列添加到表中,因为第 5 行的F3This stri98ng includes 10/15 numbers.9个数字。9 和 int 包括 4 个数值:98、10、15、0.9。 作为另一个示例,第 6 行的这 4 个数字列的值将是 45、null、null、null。

注意, 在这里我问了一个类似的问题。 该解决方案适用于我在那里提出的一般问题,但不适用于我上面描述的问题。

下面使用

select * from (
  select F1, F2, F3, offset + 1 as offset, num
  from your_table 
  left join unnest(regexp_extract_all(F3, r'([\d\.]+)')) num with offset
)
pivot (min(num) as numerical_val for offset in (1,2,3,4))     

如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

暂无
暂无

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

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