問題的一部分是,您選擇了一個可怕的列名稱,"timestamp"
。 timestamp
是一個內置的數據類型的名稱,所以使用它作爲列名,你必須"double quote"
它無處不在。
這還不是全部,雖然。你的窗口函數的語法是錯誤的。請參閱window function syntax。你忘了AND
;它是RANGE BETWEEN .. PRECEDING AND ... FOLLOWING
。
此外,雖然它不是問題的原因,但應該使用SQL標準current_timestamp
而不是now()
。
這會讓你到一個新的錯誤:
CREATE TABLE iptable (sourceip cidr, destinationip cidr, "timestamp" timestamptz);
regress=> select
sourceip,
destinationip,
timestamp,
count(*) OVER (PARTITION BY sourceip order by "timestamp" RANGE BETWEEN current_timestamp - '7 day'::Interval PRECEDING AND current_timestamp - '14 day'::Interval FOLLOWING)
from
iptable;
ERROR: RANGE PRECEDING is only supported with UNBOUNDED
LINE 5: ... OVER (PARTITION BY sourceip order by "timestamp" RANGE BETW...
^
這表明你想讓它做什麼當前窗口功能的實現不會做。可悲的是。
The value PRECEDING and value FOLLOWING cases are currently only allowed in ROWS mode. They indicate that the frame starts or ends with the row that many rows before or after the current row. value must be an integer expression not containing any variables, aggregate functions, or window functions.
相反,我只是使用普通的GROUP BY
與輸入行的WHERE
過濾器。
select
sourceip,
count(sourceip) AS n_conns_7_to_14_days_ago
from
iptable
WHERE age("timestamp") BETWEEN INTERVAL '7' DAY AND INTERVAL '14' DAY
GROUP BY sourceip;
請顯示所有問題,你的PostgreSQL版本,和錯誤消息的確切文本。 –