【发布时间】:2016-11-18 16:06:04
【问题描述】:
我有一张表,我需要使用此规则从所有内容中进行选择:
id = 4524522000143 and validPoint = true
and date > (max(date)- interval '12 month')
-- the max date, is the max date for this id
解释规则:我必须获取所有寄存器并计算它们,它们必须至少 1 年从最新的寄存器开始。
这是我的实际查询:
WITH points as (
select 1 as ct from base_faturamento_mensal
where id = 4524522000143 and validPoint = true
group by id,date
having date > (max(date)- interval '12 month')
) select sum(ct) from points
有没有更有效的方法?
【问题讨论】:
标签: sql performance postgresql group-by