I've written a query that groups metrics into 5 minute buckets and counts the number of occurences in each bucket.
This is the query:
select count(*) as amnt,
case when firmness < 90 then 'indicative' else 'executable' end as metric,
to_timestamp(floor((extract('epoch' from _received) / 300)) * 300) as time
from feedintra
where _received >= now()::date
and firmness is not null
and firmness between 0 and 90
group by firmness, time
order by time;
The results look like so:
| amnt | metric | time |
| -------- | -------------- | -------------- |
| 1584| indicative| 2022-11-16 21:25:00.000000 +00:00|
| 36290 | executable| 2022-11-16 21:25:00.000000 +00:00|
| 1250| indicative| 2022-11-16 21:25:00.000000 +00:00|
| 53074| executable| 2022-11-16 21:25:00.000000 +00:00|
What I want to do is convert the time so that it's in UTC. When I try to do this, 11 hours is added to time, presumably because PostgreSQL thinks the time is already in UTC.
select count(*) as amnt,
case when firmness < 90 then 'indicative' else 'executable' end as metric,
to_timestamp(floor((extract('epoch' from _received) / 300)) * 300) at time zone 'Australia/Sydney' at time zone 'UTC' as time
from feedintra
where _received >= now()::date
and firmness is not null
and firmness between 0 and 90
group by firmness, time
order by time;
The data now looks like this:
| amnt | metric | time |
| -------- | -------------- | -------------- |
| 1584| indicative| 2022-11-17 08:25:00.000000 +00:00|
| 36290 | executable| 2022-11-17 08:25:00.000000 +00:00|
| 1250| indicative| 2022-11-17 08:30:00.000000 +00:00|
| 53074| executable| 2022-11-17 08:30:00.000000 +00:00|
I want it to be:
| amnt | metric | time |
| -------- | -------------- | -------------- |
| 1584| indicative| 2022-11-16 10:25:00.000000 +00:00|
| 36290 | executable| 2022-11-16 10:25:00.000000 +00:00|
| 1250| indicative| 2022-11-16 10:30:00.000000 +00:00|
| 53074| executable| 2022-11-16 10:30:00.000000 +00:00|
How can I make PostgreSQL treat the time column as 'Australia/Sydney' time and then convert this to UTC?
'epoch' from _received at time zone 'Australia/Sydney'_received._receivedhappens to be atimestamptz, my initial answer needs a::timestampto cheat it.date_bin()that OP seems to be emulating through the extract+divide+floor+multiply+cast operation.