[英]How to EXTRACT from long string by SQL BigQuery
By using Bigquery SQL (#standardSQL) , I want to extract 0.1e1 and 0.55e6 to another column and convert data type float -> int64.通过使用SQL的BigQuery(#standardSQL),我想提取0.1e1和0.55e6到另一列和转换数据类型float - > Int64的。
\nsub_total:\n- !ruby/object:BigDecimal 18:0.1e1\n- !ruby/object:BigDecimal 18:0.55e6\ninvoice_number:\n- \n- '
My expected:我的预期:
String |字符串 | 1 |
1 | 550000
550000
Use regexp_extract_all()
to extract the strings.使用
regexp_extract_all()
提取字符串。 Then convert to a float and then to an integer to sum them:然后转换为浮点数,然后转换为整数以求和:
select t.*,
(select sum(cast(cast(val as float64) as int64))
from unnest(regexp_extract_all(str, '[0-9][.][0-9]*e[0-9]+')) val
)
from (select ' \nsub_total:\n- !ruby/object:BigDecimal 18:0.1e1\n- !ruby/object:BigDecimal 18:0.55e6\ninvoice_number:\n- \n- ' as str
) t;
EDIT:编辑:
If you want to extract these into an array, just use:如果要将这些提取到数组中,只需使用:
select t.*,
(select array_agg(cast(cast(val as float64) as int64))
from unnest(regexp_extract_all(str, '[0-9][.][0-9]*e[0-9]+')) val
) as int_array
from (select ' \nsub_total:\n- !ruby/object:BigDecimal 18:0.1e1\n- !ruby/object:BigDecimal 18:0.55e6\ninvoice_number:\n- \n- ' as str
) t
If you want these in separate columns, just use array operations.如果您想将它们放在单独的列中,只需使用数组操作。
EDIT:编辑:
If you want the values in separate columns:如果您想要单独列中的值:
select t.*, ar[safe_ordinal(1)] as col1,
ar[safe_ordinal(2)] as col2
from (select t.*,
array_agg(cast(cast(val as float64) as int64)) as ar
from (select ' \nsub_total:\n- !ruby/object:BigDecimal 18:0.1e1\n- !ruby/object:BigDecimal 18:0.55e6\ninvoice_number:\n- \n- ' as str
) t
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.