我试图找到总共的记录。本周和后退8周。我正在运行postgresql 9.4。我查看了这个post,但无法了解如何获得这几周。
我的数据库是darwincore2,我有一个字段" registerdate"看起来像这样" 2011-02-01"。我在提取日期时已阅读docs。我想从今年和本周开始倒数。有人有建议吗?如何在我的数据库上的SQL语句中使用此代码?
SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
我想我必须过滤当年呢?
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
请原谅我的postgresql知识!
我试过了:
select date_trunc(registerdate, 'week') as wk, count(*)
from darwincore2
where registerdate >= date_trunc('week', current_date) - '8 week'::interval
group by wk
order by wk;
但是我收到了这个错误:
ERROR: function date_trunc(character varying, unknown) is not unique
LINE 1: select date_trunc(registerdate, 'week') as wk, count(*)
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.
答案 0 :(得分:1)
如果你想使用Postgres的周定义:
where timestamp >= date_trunc('week', current_date) - '8 week'::interval
Postgres周从星期一开始,这符合ISO 8601标准。
编辑:
如果你想要每周的记录总数,那么就像这样:
select date_trunc(timestamp, 'week') as wk, count(*)
from t
where timestamp >= date_trunc('week', current_date) - '8 week'::interval
group by wk
order by wk;