sql - Presto SQL:使用由于查询而出现的时区字符串更改时区不起作用
问题描述
我通过 Mode Analytics Platform 连接到 AWS Athena 并使用其查询引擎(基于 Presto 0.172)查询表。该表public.zones
的时区信息存储在time_zone
我感兴趣的某些区域的列中,存储为varchar
.
例如,如果我输入:
SELECT time_zone
FROM public.zones
LIMIT 4;
我得到(如预期的那样):
time_zone
----------
US/Pacific
US/Eastern
US/Eastern
US/Eastern
我可以运行这个测试查询:
SELECT
timestamp '2017-06-01 12:34:56.789' AT TIME ZONE 'US/Eastern' AS time_eastern,
time_zone
FROM public.zones
LIMIT 4;
我得到(如预期的那样)
time_eastern time_zone
---------------------------------- ----------
2017-06-01 08:34:56.789 US/Eastern US/Pacific
2017-06-01 08:34:56.789 US/Eastern US/Eastern
2017-06-01 08:34:56.789 US/Eastern US/Eastern
2017-06-01 08:34:56.789 US/Eastern US/Eastern
现在,我想'2017-06-01 12:34:56.789'
在我从 zone 表中查询的不同时区表示相同的时间字符串。我希望以下查询能够运行。(它在 PostgreSQL 上运行)。
SELECT
timestamp '2017-06-01 12:34:56.789' AT TIME ZONE time_zone AS time_custom,
time_zone
FROM public.zones
LIMIT 4;
我收到以下错误:
[Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client.
line 2:52: no viable alternative at input 'TIME ZONE time_zone'
这在 Presto SQL / AWS Athena 查询引擎中不起作用的原因是什么?
任何人都可以提出任何解决方法或我的语法错误(如果有的话)是什么?
解决方案
AT TIME ZONE
只接受文字或区间。
Presto 320正是为此目的而添加with_timezone
(for timestamp
values) at_timezone
(for values)。timestamp with time zone
如果您使用的是旧版 Presto(例如撰写本文时的 Athena),您可以使用以下解决方法。您可以将时间戳值转换为 a varchar
,与 zone 连接并转换为timestamp with time zone
.
presto> select cast(cast(t as varchar) || ' ' || zone as timestamp with time zone)
from (values (timestamp '2017-06-01 12:34:56.789', 'US/Pacific')) x(t, zone);
_col0
---------------------------------------------
2017-06-01 12:34:56.789 America/Los_Angeles
(1 row)
(注意:在 Presto 320 上测试过。如果这在 Athena 上还不起作用,请告诉我。)
推荐阅读
- reactjs - ListGroup 呈现错误的值
- html - 具有两个条件的 XPath 同级
- python-3.x - 我正在尝试使用机器人将来自不和谐服务器的消息存储在数据库中
- java - 无法在 Spring Boot 测试中运行 sql 文件
- hana - 访问另一个 HDI 容器中的表函数
- powershell - 如何创建用户
- javascript - 如何隐藏命令。自定义剑道?
- elasticsearch - elasticsearch date range 获取今天和昨天之间的日期范围
- html - 如何在 Angular Material 的对话框中放置图像?
- macos - 在 macOS 上使用 Core Image 获取 RAW 图像大小的正确方法是什么