ClickHouse中的时间比较

时间:2019-09-20 17:47:18

标签: 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'

我希望得到0个结果,但得到:

2019-08-31T18:35:00+00:00

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

谢谢!

2 个答案:

答案 0 :(得分:2)

ClickHouse将DateTime存储为Unix时间戳-不含时区的单词。 但是执行sql-query时会考虑时区:

SELECT
    toDateTime('2019-08-31 20:35:00', 'UTC') AS origin_date,

    toTimeZone(origin_date, 'Etc/GMT+2') AS d1,
    toTypeName(d1) AS type1,
    toUnixTimestamp(d1) AS t1,

    toTimeZone(origin_date, 'UTC') AS d2,
    toTypeName(d2) AS type2,
    toUnixTimestamp(d2) AS t2
FORMAT Vertical

Row 1:
──────
origin_date: 2019-08-31 20:35:00

d1:          2019-08-31 18:35:00
type1:       DateTime('Etc/GMT+2')
t1:          1567283700 # <-- t1 == t2

d2:          2019-08-31 20:35:00
type2:       DateTime('UTC')
t2:          1567283700 # <-- t1 == t2

您的查询正常工作。

要以这种方式使用“重置 z -date的时区”:

SELECT toDateTime(toString(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'

答案 1 :(得分:1)

TZ是类型的属性,而不是值

DESCRIBE TABLE
(
    SELECT
        toTimeZone(toDateTime('2019-08-31 20:35:00'), 'Etc/GMT+2') AS x,
        toDateTime('2019-08-31 20:35:00') AS y
)

┌─name─┬─type──────────────────┬─
│ x    │ DateTime('Etc/GMT+2') │
│ y    │ DateTime              │
└──────┴───────────────────────┴─


SELECT toTimeZone(ts, 'Etc/GMT+2') AS z
FROM
(
    SELECT toDateTime('2019-08-31 20:35:00') AS ts
)
WHERE z > toDateTime('2019-08-31 20:34:00', 'Etc/GMT+2')

Ok.

0 rows in set. Elapsed: 0.002 sec.