Difference() gets modified on a subquery - bug or feature()

As I was struggling with my earlier question, I thought I came up with a solution. But my hopes were dashed.

Here’s level 1 of SELECT statement:

select difference(count(value1)) as DCT, count(value1) as CT, first(value1) as FC from ABC where time>‘2017-12-14’ group by time(1d,630m) tz(‘America/Chicago’)
name: ABC
time DCT CT FC


2017-12-13T10:30:00-06:00 592 592 2.39
2017-12-14T10:30:00-06:00 563 1155 2.46
2017-12-15T10:30:00-06:00 -836 319 2.715
2017-12-16T10:30:00-06:00 -319 0
2017-12-17T10:30:00-06:00 764 764 2.6
2017-12-18T10:30:00-06:00 302 1066 2.47
2017-12-19T10:30:00-06:00 -714 352 2.445
2017-12-20T10:30:00-06:00 -352 0

Now, I would like to select only those rows where CT>0 and DCT!=CT. However, value of DCT column changes mid-flight:

select * from (select difference(count(value1)) as DCT, count(value1) as CT, first(value1) as FC from ABC where time>‘2017-12-14’ group by time(1d,630m) tz(‘America/Chicago’)) where CT>0
name: ABC
time CT DCT FC


2017-12-13T16:30:00Z 778 404 2.425
2017-12-14T16:30:00Z 1155 30 2.46
2017-12-15T16:30:00Z 319 -836 2.715
2017-12-17T16:30:00Z 764 445 2.6
2017-12-18T16:30:00Z 1066 302 2.47
2017-12-19T16:30:00Z 352 -714 2.445

Notice that DCT in row for 2017-12-17 is no longer 764, but 445! As a result, the following query produces totally unexpected result:

select * from (select difference(count(value1)) as DCT, count(value1) as CT, first(value1) as FC from “ABC” where time>‘2017-12-14’ group by time(1d,630m) tz(‘America/Chicago’)) where CT>0 and DCT!=CT
name: ABC
time CT DCT FC


2017-12-13T16:30:00Z 778 404 2.425
2017-12-14T16:30:00Z 1155 30 2.46
2017-12-15T16:30:00Z 319 -836 2.715
2017-12-17T16:30:00Z 764 445 2.6
2017-12-18T16:30:00Z 1066 302 2.47
2017-12-19T16:30:00Z 352 -714 2.445