【发布时间】:2017-09-28 20:37:03
【问题描述】:
我正在努力处理可以根据Where 子句中的输入参数动态转换timezone 的查询。它应该能够根据WITH 中的键和值确定适当的时区转换。系统时区是 UTC。
我有大约 8 个时区可供使用,并正在寻找一种更智能的方法来检测转换,而不必总是更改 At Time Zone 'pst'
这是我到目前为止的内容,但语法需要一些帮助:
with
data(id, timezone) AS (values
(2, 'US/Pacific'),
(5, 'US/Mountain'),
(10, 'US/Eastern'))
select data.id, marg.timestamp, marg.value, structure.market
from data, marg, structure
where id in (2,5,10)
and marg.timestamp between '2017-01-01' and '2017-01-31'
另外,如果有更好的方法来实现这一点,我愿意接受反馈。
理想情况下,输出应如下所示:
utc |id| timezone | local_time | value
--------------------+--+------------+---------------------- +----------------
2017-01-01 22:19:36 |2 | US/Pacific | 2017-01-01 15:19:36 | 2435
2017-01-10 22:29:36 |2 | US/Pacific | 2017-01-10 15:29:36 | 215
2017-01-30 22:39:36 |2 | US/Pacific | 2017-01-30 15:29:36 | 2150
2017-01-28 22:19:36 |5 | US/Mountain| 2017-01-28 16:19:36 | 11341
2017-01-29 22:19:36 |5 | US/Mountain| 2017-01-29 16:19:36 | 131
2017-01-04 22:19:36 |10| US/Mountain| 2017-01-04 16:19:36 | 134
2017-01-05 22:19:36 |10| US/Eastern | 2017-01-05 18:19:36 | 2451
它采用 UTC 中的系统时间并转换为适当的时区 local_time
【问题讨论】:
标签: sql postgresql timezone