sql - Redshift - 在 where 子句中将 UTC 时间转换为本地时间时出错
问题描述
我有一些以 UTC 记录的销售数据。我正在尝试将其转换为发生销售的当地时区。
我已经建立了一个如下查询,但收到一个错误,提示操作无效:函数 to_char(timestamp without time zone, charcter varying, unknown") 不存在。
select fs.sale_id,fs.store_type,fs.sale_time ,
case when fs.timezone = 'BST' then dateadd( h, 1, fs.sale_time ) when fs.timezone = 'EDT' then dateadd( h,- 4, fs.sale_time ) when fs.timezone = 'CEST' then dateadd( h, 2, fs.sale_time ) when fs.timezone = 'EEST' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'MSK' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'WEST' then dateadd( h, 1, fs.sale_time ) else null end, fs.timezone as new_time
from sales fs
where to_char((case when fs.timezone = 'BST' then dateadd( h, 1, fs.sale_time ) when fs.timezone = 'EDT' then dateadd( h,- 4, fs.sale_time ) when fs.timezone = 'CEST' then dateadd( h, 2, fs.sale_time ) when fs.timezone = 'EEST' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'MSK' then dateadd( h, 3, fs.sale_time ) when fs.timezone = 'WEST' then dateadd( h, 1, fs.sale_time ) else null end, fs.timezone),'yyyy-mm-dd') = '2018-09-01'
谁能建议我如何修改此查询。我正在使用红移数据库。谢谢。
解决方案
我建议使用 'CONVERT_TIMEZONE' 函数,详细信息如下所示。
https://docs.aws.amazon.com/redshift/latest/dg/CONVERT_TIMEZONE.html
例如,将销售额从 UTC 转换为 EST 的简单查询如下所示。
select listtime, convert_timezone('PST', listtime) from listing where listid = 16;
它将返回如下所示的内容。
listtime | convert_timezone
--------------------+-------------------
2008-08-24 09:36:12 2008-08-24 01:36:12
推荐阅读
- sbt - 如何使用类路径从 sbt 运行代码?
- node.js - mongodb+srv URI 不能有端口号
- r - 尽管生成器是无限循环,但张量流“您的输入已用完数据”错误
- node.js - pg-connect-simple 抛出“无法修剪会话:证书链中的自签名证书”
- reactjs - React 自定义控件返回值
- algorithm - 需要一个算法/逻辑来逐步执行步骤并分配变量,伪代码很好
- html - 当我压缩、发送和解压缩我的项目时,css 图像不会出现
- python - AttributeError:模块'discord'没有属性'ui'
- python - 我可以在 python 中加速这个列表生成吗?
- python - 使用csv在python中保存数据