我正在尝试执行无法弄清楚如何写的查询。我有一个claims
桌子
claims table:
- id
- date_received
我想获取30天内每天的新索赔计数。我想提出的幼稚解决方案就是这个。
select count(*)
from claims
where date_received
BETWEEN CURRENT_DATE - interval '1 month'
AND CURRENT_DATE
group by date_received;
这有效,但是它按确切的时间戳而不是同一天进行分组。我如何才能在同一天将其分组?
编辑
我能够找到我们更新的查询:
select date_received::date as date, count(id) as new_claims
from claims
where date_received BETWEEN CURRENT_DATE - interval '1 month'
AND CURRENT_DATE
group by date_received::date
order by date;
答案 0 :(得分:0)
大概,索赔到达的是过去的时间,而不是将来的发生时间,所以您需要:
select date_received::date, count(*)
from claims
where date_received >= CURRENT_DATE - interval '1 month' and
date_received < CURRENT_DATE
group by date_received::date
order by date_received::date;
答案 1 :(得分:0)
基于戈登的答案,您可以使用date_trunc(precision, timestamp)
来获取所需的内容:
select date_received::date, count(*)
from claims
where date_received >=
date_trunc('day', CURRENT_DATE - interval '1 month')
and date_received < CURRENT_DATE
group by date_received::date
order by date_received::date;
有关详细信息,请参见date_trunc()。