ClickHouse 中的时间比较

Vya*_*lav 3 clickhouse

也许我错过了一些简单的事情,但我无法让时间过滤工作。

这是我的示例查询:

select toTimeZone(ts, 'Etc/GMT+2') as z
from (select toDateTime('2019-08-31 20:35:00') AS ts)
where z > '2019-08-31 20:34:00'
Run Code Online (Sandbox Code Playgroud)

我期望 0 结果,但得到:

2019-08-31T18:35:00+00:00
Run Code Online (Sandbox Code Playgroud)

这是一个错误,还是我滥用了 toTimeZone() 函数?

谢谢!

vla*_*mir 5

ClickHouse 将 DateTime 存储为 Unix 时间戳 - 换句话说,没有时区。\n但是执行 sql 查询时会考虑时区:

\n\n
SELECT\n    toDateTime(\'2019-08-31 20:35:00\', \'UTC\') AS origin_date,\n\n    toTimeZone(origin_date, \'Etc/GMT+2\') AS d1,\n    toTypeName(d1) AS type1,\n    toUnixTimestamp(d1) AS t1,\n\n    toTimeZone(origin_date, \'UTC\') AS d2,\n    toTypeName(d2) AS type2,\n    toUnixTimestamp(d2) AS t2\nFORMAT Vertical\n\nRow 1:\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\norigin_date: 2019-08-31 20:35:00\n\nd1:          2019-08-31 18:35:00\ntype1:       DateTime(\'Etc/GMT+2\')\nt1:          1567283700 # <-- t1 == t2\n\nd2:          2019-08-31 20:35:00\ntype2:       DateTime(\'UTC\')\nt2:          1567283700 # <-- t1 == t2\n
Run Code Online (Sandbox Code Playgroud)\n\n

您的查询工作正常。

\n\n

要“重置z -date 的时区”,可以这样使用:

\n\n
SELECT toDateTime(toString(toTimeZone(ts, \'Etc/GMT+2\'))) AS z\nFROM\n(\n    SELECT toDateTime(\'2019-08-31 20:35:00\') AS ts\n)\nWHERE z > \'2019-08-31 20:34:00\'\n
Run Code Online (Sandbox Code Playgroud)\n