我想知道是否有辦法在 PostgreSQL 中找到“中位數”日期。目標是根據中位數獲得所有日期中間的日期。
我嘗試了以下修改的中值函式:
select
percentile_cont(0.5) within group (order by date)
from cte
通過嘗試這樣做,我收到以下錯誤訊息:
SQL Error [42883]: ERROR: function percentile_cont(numeric, timestamp without time zone) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 12
由于不支持日期,我想知道是否有另一種方法來計算日期的中值。
感謝您的任何投入!
uj5u.com熱心網友回復:
您可以將日期值轉換為整數,然后使用該percentile_cont函式將其用于獲取中值。像這樣,
SELECT
percentile_cont(0.5) within group (ORDER by cast(extract(epoch from dateCol1) as integer))
FROM table1
上面給出了中間日期,但在數值中,要將其轉換回日期型別,請使用如下to_timestamp函式,
select to_timestamp(1638662400)::date
#gives 2021-12-05
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408046.html
標籤:
