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/

相关文章:

php - 连接两个 mysql_fetch_array

php - 在 php 中取消转义双引号

mysql - 尝试创建复杂 SQL 查询的新手 SQL 用户

sql - 用于确定表值函数中返回哪个 SELECT 的 If 语句

c - Valgrind Memcheck for PostgreSQL C 函数

ruby-on-rails - rails : How to store date in "Chennai" Timezone in the database

timezone - php 5.2.17 date ('T' ) 给出的时区不正确。服务器时间+phpinfo时间正确

javascript - 如何根据客户位置更改时区

SQL IF/ELSE 语句

php - Postgres : update all values in column by one?