我已經在 Postgres 中使用這個查詢一個月了,它一直運行良好:
SELECT
DATE_TRUNC('hour', created_at::timestamp) AS datetime,
AVG(temperature_1) as temperature_1
FROM
main_data
WHERE
created_at BETWEEN '2022-01-22 01:00:00' AND '2022-01-22 3:00:00'
GROUP BY
DATE_TRUNC('hour', created_at)
現在,當我使用查詢時,出現此錯誤:
ERROR: column "main_data.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: DATE_TRUNC('hour', created_at::timestamp) as datetime,
^
SQL state: 42803
Character: 27
我沒有做任何更新。

這是我的 Postgres 資料庫中的 created_at 欄位
uj5u.com熱心網友回復:
使用group by關鍵字時,您的列格式(使用演員表等)應該在分組操作中使用相同的格式
SELECT
DATE_TRUNC('hour', created_at::timestamp) as datetime,
AVG(temperature_1) as temperature_1
FROM main_data
where created_at BETWEEN '2022-01-22 01:00:00' AND '2022-01-22 3:00:00'
GROUP BY DATE_TRUNC('hour', created_at::timestamp)
uj5u.com熱心網友回復:
如果 created_at 的型別更改為 not ,就會發生這種情況timestamp。例如,timestamptz。
示范
鑄造應該是不必要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418720.html
標籤:
