I have a table (of fake data) with a timestamp column. These values behave exactly as I expect.
SELECT timestamp, to_timezone(timestamp, 'EST') as timezone_converted, dateadd('d', 1, timestamp) as day_added from events;
https : // imgur . com / XJDnJaX
I have generated a reference series, with one value per day (I basically just want to figure out the number of events per day), starting from today and counting backwards, as follows:
SELECT
timestamp_sequence(date_trunc('day', now()), -86400000000L) as reference_date
FROM
long_sequence(300)
So far so good:
When I try to operate on the generated dates I have no idea what’s going on.
WITH d AS (
SELECT
timestamp_sequence(date_trunc('day', now()), -86400000000L) as reference_date
FROM
long_sequence(300)
)
SELECT
reference_date,
to_timezone(reference_date, 'EST') as timezone_converted,
dateadd('d', 1, reference_date) as day_added
FROM
d
ORDER BY
reference_date;
The series is generated exactly the same way, and it should be all values 1 day apart going back the past 300 days. But somehow, operating on it changes the values?
Here is the kind of thing I really want to do:
WITH d AS (
SELECT
timestamp_sequence(date_trunc('day', now()), -86400000000L) as reference_date
FROM
long_sequence(300)
)
SELECT
d.reference_date,
COALESCE(SUM(e.amount), 0) AS total_amount
FROM
d
LEFT JOIN
events e
ON
date_trunc('day', e.timestamp) = d.reference_date
GROUP BY
d.reference_date
ORDER BY
d.reference_date;
https : // imgur . com / GJUpEfx
I have no idea what’s going on. What am I misunderstanding? I get the same results (dates going back to 1777) using BETWEEN ... AND ...
. Is there something about the way the generated timestamps are represented that I need to know?
Apologies for obfuscated imgur links, my account is new and my options are limited.