sql - 急速 SQL : Changing time zones using time zone string coming as a result of a query is not working

标签 sql postgresql timezone amazon-athena presto

我正在通过模式分析平台连接到 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'。我希望运行以下查询。 (它在 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 (对于 timestamp 值)at_timezone (对于 timestamp with time zone 值)正是为了这个目的。

如果您使用的是较旧的 Presto 版本(例如撰写本文时的 Athena),则可以使用以下解决方法。您可以将时间戳值转换为 varchar,与区域连接并转换为 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 上还不起作用,请告诉我。)

关于sql - 急速 SQL : Changing time zones using time zone string coming as a result of a query is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52230931/

相关文章:

sql - Postgres 创建一个带有外键数组的表

mysql - 如何按所选顺序选择具有最小开始日期和最大结束日期的所有工作人员?

postgresql - 如何使用 postgres crate 将表名作为变量传递给 execute()?

ruby-on-rails - 解决 PG::GroupingError: ERROR

sql - 将行拆分为具有不同列的表

java - 将时间戳转换为 UTC 时区

java - 如何从 Java 语言环境的时区获取日期模式

sql - Transact-sql 中的条件运算符

c# - 使用野田时间获取给定偏移量(以分钟为单位)的时区列表

sql - 在 SQL 中从 XML 中选择值