提问人:Siddarth Patil 提问时间:11/10/2023 最后编辑:Siddarth Patil 更新时间:11/10/2023 访问量:63
AWS Athena 查询将复杂字符串列转换为整数
AWS Athena Query to convert complex string column to integer
问:
我有一个 Athena 表,其中有一列如下所示:string
+-------------------+
| employee_size |
+-------------------+
| GREATER THAN 2000 |
+-------------------+
| 500 - 999 |
+-------------------+
| 28.00 |
+-------------------+
| unknown |
+-------------------+
| 563 |
+-------------------+
如果可能的话,我想将列值转换为,否则应该是.因此,所需的输出应如下所示:integer
null
+---------------+
| employee_size |
+---------------+
| |
+---------------+
| |
+---------------+
| 28 |
+---------------+
| |
+---------------+
| 563 |
+---------------+
我尝试使用我认为最接近的查询组合:
SELECT
CASE
WHEN employee_size LIKE '% %' THEN NULL
WHEN employee_size LIKE '%-%' THEN NULL
WHEN regexp_like(employee_size,'([A-Za-z]') THEN NULL
WHEN employee_size LIKE '%.%' THEN CAST(employee_size AS decimal)
ELSE CAST(employee_size AS integer)
END AS employee_size
FROM
"table_name";
但此代码会导致错误:
INVALID_FUNCTION_ARGUMENT:带有不匹配括号的结束模式
如果你们遇到过类似的事情,请提出一个解决方案。
编辑:我忘了提到,如果有像或这样的值,它应该忽略小数点后的任何内容,只有或decimal
28.00
5.64
28
5
答:
0赞
Tim Biegeleisen
11/10/2023
#1
您可以尝试以下逻辑:
SELECT
CASE WHEN REGEXP_LIKE(employee_size, '^[0-9]+(\.[0-9]+)?$')
THEN REGEXP_EXTRACT(employee_size, '^[0-9]+') END AS employee_size
FROM yourTable;
1赞
Igor T
11/10/2023
#2
你在寻找这样的东西吗?
with
t as (
select 'GREATER THAN 2000' employee_size
union all
select '500 - 999' employee_size
union all
select '28.00' employee_size
union all
select 'unknown' employee_size
union all
select '563' employee_size
)
select cast(try_cast(employee_size as double) as integer) from t
评论
12.34
decimal
integer
decimal