我正在使用一个数据集,该数据集的列标题为"birth_dt"从中我试图计算18个月前成员的年龄。
= select distinct(birth_dt) from table.DB limit 9;显示列如下所示:
birth_dt
-----------------------
1990-08-15 00:00:00.000
1986-05-30 00:00:00.000
1981-03-03 00:00:00.000
1970-05-11 00:00:00.000
1963-01-20 00:00:00.000
1985-01-26 00:00:00.000
1983-01-03 00:00:00.000
2021-01-03 00:00:00.000
1995-11-14 00:00:00.000
在Athena中,该列显示为"字符串"。
到目前为止,我所尝试的是:
select DATE_DIFF('year', birth_dt, (current_timestamp - interval '18' month)) as age from table.DB limit 10;
一直给我一个错误:
"SYNTAX_ERROR: line 1:8:函数date_diff的意外参数(varchar(4), varchar, timestamp with time zone)。期望:date_diff(varchar(x), time, time), date_diff(varchar(x), time with time zone, time with time zone), date_diff(varchar(x), timestamp with time zone, timestamp with time zone), date_diff(varchar(x), date, date), date_diff(varchar(x), timestamp, timestamp)">
然后我试着把注意力集中在获得"birth_dt"列更改为有效日期无效:
select date_parse(birth_dt, '%YYYY-%MM-%dd') as age from table.DB limit 10;
select cast(from_iso8601_timestamp("birth_dt.1") AS date) as age from table.DB limit 10;
SELECT date_parse(birth_dt,'%Y-%m-%dT%H:%i:%s%+00:00') as date_column,
current_timestamp as Todays_Date,
date_diff('day',current_timestamp,date_parse(birth_dt,'%Y-%m-%dT%H:%i:%s%+00:00')) as difference
FROM table.DB limit 10;
这些查询最终会给我错误,说:
INVALID_FUNCTION_ARGUMENT:无效格式:"1990-08-15 00:00:00.000">
-08-15 00:00:00.000
或
INVALID_FUNCTION_ARGUMENT:无效格式:"1990-08-15 00:00:00.000"在"00:00:00.000";
我想在上面的例子中最终得到的是:
Age
---
31
35
40
51
58
36
38
1
25
请帮忙!谢谢你! !
提供有效的格式在这里至关重要。有些尝试非常接近,请尝试以下操作:
-- sample data
WITH dataset(birth_dt) AS (
values ('1983-01-03 00:00:00.000'),
('2021-01-03 00:00:00.000')
)
-- query
select date_diff(
'year',
date_parse(birth_dt, '%Y-%m-%d %T.%f'),
current_timestamp - interval '18' month)
from dataset;
输出: