Postgress在postgress的時間戳幾個tipes:
時間戳沒有時區 - (優先存儲UTC時間戳)您在跨國數據庫存儲找到它。在這種情況下,客戶端將處理每個國家/地區的時區偏移量。
帶時區的時間戳 - 時區已包含在時間戳中。
在某些情況下,你的數據庫不使用的時區,但你仍然需要組記錄與當地時區和夏令時的尊重(如https://www.timeanddate.com/time/zone/romania/bucharest)
要添加時區,你可以用這個例子並更換時區偏移與你的。
"your_date_column" at time zone '+03'
要添加特定於夏令時的+1夏令時偏移,您需要檢查您的時間戳是否屬於夏令時。由於這些間隔隨着1或2天不等,我將使用不會影響月末記錄的aproximation,因此在這種情況下,我可以忽略每年的確切時間間隔。
如果需要構建更精確的查詢,則必須添加條件以創建更多案例。但是,粗略計算,這些將正常工作每月分割數據與時區和夏令尊重,當你發現沒有時間戳在數據庫中時區:
SELECT
"id", "Product", "Sale",
date_trunc('month',
CASE WHEN
Extract(month from t."date") > 03 AND
Extract(day from t."date") > 26 AND
Extract(hour from t."date") > 3 AND
Extract(month from t."date") < 10 AND
Extract(day from t."date") < 29 AND
Extract(hour from t."date") < 4
THEN
t."date" at time zone '+03' -- Romania TimeZone offset + DST
ELSE
t."date" at time zone '+02' -- Romania TimeZone offset
END) as "date"
FROM
public."Table" AS t
WHERE 1=1
AND t."date" >= '01/07/2015 00:00:00'::TIMESTAMP WITHOUT TIME ZONE
AND t."date" < '01/07/2017 00:00:00'::TIMESTAMP WITHOUT TIME ZONE
GROUP BY date_trunc('month',
CASE WHEN
Extract(month from t."date") > 03 AND
Extract(day from t."date") > 26 AND
Extract(hour from t."date") > 3 AND
Extract(month from t."date") < 10 AND
Extract(day from t."date") < 29 AND
Extract(hour from t."date") < 4
THEN
t."date" at time zone '+03' -- Romania TimeZone offset + DST
ELSE
t."date" at time zone '+02' -- Romania TimeZone offset
END)
向我們展示你的企圖 –
你有沒有問這裏之前嘗試新鮮事物。如果嘗試添加細節,否則不要問。 – ST3