Simple query fail

Started by Glenn Pierceover 8 years ago3 messagesgeneral
Jump to latest
#1Glenn Pierce
glennpierce@gmail.com

Hi so I have a simple table as

\d sensor_values_days;
Table "public.sensor_values_days"
Column | Type | Modifiers
-----------+--------------------------+------------------------------
ts | timestamp with time zone | not null
value | double precision | not null default 'NaN'::real
sensor_id | integer | not null
Indexes:
"timestamp_id_index" UNIQUE CONSTRAINT, btree (ts, sensor_id)
Foreign-key constraints:
"sensor_values_days_sensor_id_fkey" FOREIGN KEY (sensor_id)
REFERENCES sensors(id)

and I have a simple query that fails

Ie

SELECT sensor_id, MAX(ts), date_trunc('day', ts), COALESCE(MAX(value),
'NaN')::float FROM sensor_values_days WHERE ts > '2017-10-06
00:01:01+00' AND ts < '2017-10-06 23:59:59+00' GROUP BY 1, 3 ORDER BY
1, 2;
sensor_id | max | date_trunc | coalesce
-----------+-----+------------+----------
(0 rows)

If I remove the timezone part of the start date I get results.

Ie

SELECT sensor_id, MAX(ts), date_trunc('day', ts), COALESCE(MAX(value),
'NaN')::float FROM sensor_values_days WHERE ts > '2017-10-06 00:01:01'
AND ts < '2017-10-06 23:59:59+00' GROUP BY 1, 3 ORDER BY 1, 2;
sensor_id | max | date_trunc | coalesce
-----------+------------------------+------------------------+----------
597551 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 13763
597552 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 8168
597553 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 9441
....
...
..

I'm sure I am doing something silly but can't see what.
Does anyone know what is going on here ?

I am using Postgres 9.5

Thanks

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Glenn Pierce (#1)
Re: Simple query fail

On Tue, Oct 17, 2017 at 2:29 PM, Glenn Pierce <glennpierce@gmail.com> wrote:

and I have a simple query that fails

​This is not failure, this is a query that found zero matching records.

Ie

SELECT sensor_id, MAX(ts), date_trunc('day', ts), COALESCE(MAX(value),
'NaN')::float FROM sensor_values_days WHERE ts > '2017-10-06
00:01:01+00' AND ts < '2017-10-06 23:59:59+00' GROUP BY 1, 3 ORDER BY
1, 2;
sensor_id | max | date_trunc | coalesce
-----------+-----+------------+----------
(0 rows)

If I remove the timezone part of the start date I get results.

Ie

ts > '2017-10-06 00:01:01'

597551 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 13763

I'm sure I am doing something silly but can't see what.

Does anyone know what is going on here ?

​The "max(ts)" result indicates a time of midnight, the 6th, GMT

ts > '2017-10-06 00:01:01+01' equates to > '2017-10-05 23:01:01+00' of
which midnight, the 6th, GMT is indeed more recent

ts > '2017-10-06 00:01:01+00' is 12:01:01 on the 6th, GMT, of which
midnight GMC, the 6th is NOT more recent

David J.

#3Glenn Pierce
glennpierce@gmail.com
In reply to: Glenn Pierce (#1)
Re: Simple query fail

Ok I needed a ::timestamptz at time zone 'UTC' and a >= :)

On 17 October 2017 at 22:29, Glenn Pierce <glennpierce@gmail.com> wrote:

Hi so I have a simple table as

\d sensor_values_days;
Table "public.sensor_values_days"
Column | Type | Modifiers
-----------+--------------------------+------------------------------
ts | timestamp with time zone | not null
value | double precision | not null default 'NaN'::real
sensor_id | integer | not null
Indexes:
"timestamp_id_index" UNIQUE CONSTRAINT, btree (ts, sensor_id)
Foreign-key constraints:
"sensor_values_days_sensor_id_fkey" FOREIGN KEY (sensor_id)
REFERENCES sensors(id)

and I have a simple query that fails

Ie

SELECT sensor_id, MAX(ts), date_trunc('day', ts), COALESCE(MAX(value),
'NaN')::float FROM sensor_values_days WHERE ts > '2017-10-06
00:01:01+00' AND ts < '2017-10-06 23:59:59+00' GROUP BY 1, 3 ORDER BY
1, 2;
sensor_id | max | date_trunc | coalesce
-----------+-----+------------+----------
(0 rows)

If I remove the timezone part of the start date I get results.

Ie

SELECT sensor_id, MAX(ts), date_trunc('day', ts), COALESCE(MAX(value),
'NaN')::float FROM sensor_values_days WHERE ts > '2017-10-06 00:01:01'
AND ts < '2017-10-06 23:59:59+00' GROUP BY 1, 3 ORDER BY 1, 2;
sensor_id | max | date_trunc | coalesce
-----------+------------------------+------------------------+----------
597551 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 13763
597552 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 8168
597553 | 2017-10-06 01:00:00+01 | 2017-10-06 00:00:00+01 | 9441
....
...
..

I'm sure I am doing something silly but can't see what.
Does anyone know what is going on here ?

I am using Postgres 9.5

Thanks

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general