如何将VARCHAR列转换为表中的时间戳

时间:2020-07-08 10:17:31

标签: amazon-athena apache-superset

我正在尝试将表中的VARCHAR列转换为TIMESTAMP

当前,我的专栏看起来像这样:

2020-05-31T05:00:21Z

我的目标是将此列转换为时间戳,以便将其用于可视化(带有趋势线的大数字)

当我将列保持原样时,会出现此错误:

awsathena error: INVALID_CAST_ARGUMENT: Value cannot be cast to timestamp: 2020-07-07T18:56:56Z

当我将列的类型更改为TIMESTAMP时,出现了另一个错误:

awsathena error: SYNTAX_ERROR: line 4:19: '>=' cannot be applied to varchar, timestamp with time zone

这是可视化中的查询:

SELECT date_trunc('day', CAST("eventtime" AS TIMESTAMP)) AS "__timestamp",
       COUNT(*) AS "count"
FROM "cloudtrail_logs_cloud_trail_elk"
WHERE "eventtime" >= '2020-07-01 00:00:00.000000'
  AND "eventtime" < '2020-07-08 00:00:00.000000'
GROUP BY date_trunc('day', CAST("eventtime" AS TIMESTAMP))
ORDER BY "count" DESC
LIMIT 50000;

是否可以更改此列的类型,以便可以将其用于可视化?

2 个答案:

答案 0 :(得分:1)

如果有人遇到与我相同的问题,可以使用此功能:

from_iso8601_timestamp(eventtime)

在此处的Superset中:Superser-> Edit Table-> Edit Column-> Expression

Superser-> Edit Table-> Expression

答案 1 :(得分:0)

一种可能性是对日期时间值进行强制转换:

SELECT date_trunc('day', CAST("eventtime" AS TIMESTAMP)) AS "__timestamp",
       COUNT(*) AS "count"
FROM "cloudtrail_logs_cloud_trail_elk"
WHERE "eventtime" >= TIMESTAMP '2020-07-01 00:00:00'
  AND "eventtime" < TIMESTAMP '2020-07-08 00:00:00'
GROUP BY date_trunc('day', CAST("eventtime" AS TIMESTAMP))
ORDER BY "count" DESC
LIMIT 50000;

或使用between代替>=<

SELECT date_trunc('day', CAST("eventtime" AS TIMESTAMP)) AS "__timestamp",
       COUNT(*) AS "count"
FROM "cloudtrail_logs_cloud_trail_elk"
WHERE "eventtime" between TIMESTAMP '2020-07-01 00:00:00'
  AND TIMESTAMP '2020-07-08 00:00:00'
GROUP BY date_trunc('day', CAST("eventtime" AS TIMESTAMP))
ORDER BY "count" DESC
LIMIT 50000;