PSQL:我的主表有 2000 万条记录,当我运行我的选择时,它会运行几个小时。有没有办法更好地写这个陈述?
我的桌子:
Select * from lookup limit 10;
-------------------------
month id
2010-01 598362
2010-01 598343
2010-02 598343
2010-02 988343
2010-03 789624
2010-04 789624
2010-05 789624
2010-06 899624
从我试图找到的表中
- 该月的不同 ID 计数
- 在过去 2 个月中也存在的不同 ID 的计数
我的选择语句(如下)适用于小数据(最多 100,000 条记录)
--PSQL
select month,
count (distinct id)id_ct,
count (distinct case when (
id||(month-1) in (select distinct id||month from lookup )
or id||(month-2) in (select distinct id||month from lookup ) )
then id end) continuous_ct
from lookup
group by 1 order by 1
结果 :
month id_ct continuous_ct
2010-01 2 0
2010-02 2 1
2010-03 1 0
2010-04 1 1
2010-05 1 1
2010-06 1 0
谢谢!