问题描述
我正在使用以下SELECT语句计算加权移动平均值:
SELECT ts_g as ts,runningAccumulate(tpv) / runningAccumulate(tvol) as vwap
FROM
(
SELECT ts_g,sumState(pv) tpv,sumState(vol_per_price) tvol
FROM (
SELECT ts_g,close * vol_per_price as pv,sum(vol) as vol_per_price
FROM tablename_here
WHERE ts >= toDateTime64('2018-02-04 14:30:00.000000',6,'UTC')
AND ts < toDateTime64('2019-02-27 23:59:00.000000','UTC')
GROUP BY toStartOfInterval(ts,INTERVAL 1 minute) AS ts_g,close
ORDER BY ts_g ASC,close ASC
)
GROUP BY ts_g
ORDER BY ts_g
)
哪个会产生正确的结果(下面的前十行):
┌──────────────────ts─┬──────vwap─┐
│ 2018-02-05 18:05:00 │ 2742.0000 │
│ 2018-02-05 21:54:00 │ 2706.3333 │
│ 2018-02-05 23:49:00 │ 2686.0000 │
│ 2018-02-05 23:51:00 │ 2675.8500 │
│ 2018-02-06 11:56:00 │ 2664.8750 │
│ 2018-02-06 14:34:00 │ 2660.6071 │
│ 2018-02-06 15:35:00 │ 2658.6562 │
│ 2018-02-07 16:25:00 │ 2667.4722 │
│ 2018-02-09 14:53:00 │ 2663.2250 │
│ 2018-02-16 13:23:00 │ 2671.6590 │
└─────────────────────┴───────────┘
我想按如下所示选择该响应的一个子集:
SELECT ts,vwap
FROM ( original query here )
WHERE ts >= toDateTime64('some start date','UTC')
AND ts < toDateTime64('some end date','UTC')
ORDER BY ts
但是,即使WHERE子句中的日期范围等于或大于原始的日期范围,它始终返回0 rows in set
。我做错了吗?还是Clickhouse中的错误?
版本:
ClickHouse server version 20.6.3.28 (official build).
ClickHouse client version 20.6.3.28 (official build).
解决方法
您尝试将 DateTime 与 DateTime64 进行比较,从而隐式地应用了数值比较。
它需要将 DateTime 显式转换为 DateTime64 (对于分钟间隔没有意义):
SELECT ts,vwap
FROM ( original query here )
WHERE toDateTime64(ts,6,'UTC') >= toDateTime64('some start date','UTC')
AND toDateTime64(ts,'UTC') < toDateTime64('some end date','UTC')
ORDER BY ts
或在WHERE子句中使用 DateTime 值:
SELECT ts,vwap
FROM ( original query here )
WHERE ts >= toDateTime('some start date','UTC')
AND ts < toDateTime('some end date','UTC')
ORDER BY ts
我添加了与此比较的混乱行为有关的问题CH: Comparison DateTime64 to DateTime / Date。