如何从大查询 table 中的字符串列中提取所有数值并将它们插入新的数值列中?
How to extract all numerical values from a string column in a big query table and insert them in new numerical columns?
假设我有一个 table 像 temp_table
:
CREATE TABLE `YOUR_DATASET.temp_table` (
`F1` STRING,
`F2` INT64,
`F3` STRING,
);
这 table 包括一些数据:
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.temp_table`
我想编写一个大型查询脚本来提取 F3
中的所有数值,并将它们作为新的数值列附加到 temp_table
。新数值列的数量应等于 F3
中数值的最大数量。在此示例 table、temp_table
中,应该向 table 添加 4 个新的数字列,因为第 5 行的 F3
是 This stri98ng includes 10/15 numbers .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))
如果应用于您问题中的示例数据 - 输出为
假设我有一个 table 像 temp_table
:
CREATE TABLE `YOUR_DATASET.temp_table` (
`F1` STRING,
`F2` INT64,
`F3` STRING,
);
这 table 包括一些数据:
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.temp_table`
我想编写一个大型查询脚本来提取 F3
中的所有数值,并将它们作为新的数值列附加到 temp_table
。新数值列的数量应等于 F3
中数值的最大数量。在此示例 table、temp_table
中,应该向 table 添加 4 个新的数字列,因为第 5 行的 F3
是 This stri98ng includes 10/15 numbers .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))
如果应用于您问题中的示例数据 - 输出为