PSQL:如何通过另一列获取列组中每个值的记录计数

问题描述 投票:0回答:1

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 

从我想要找到的表中

  1. 那个月的不同ID的数量
  2. 在过去2个月中也存在的不同身份的计数

我的选择语句(如下)适用于小数据(最多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

谢谢!

mysql sql psql postgrest
1个回答
0
投票

如果你有一个id的索引,那么这个简单的查询应该工作。刚刚离开桌子加入自己。 http://sqlfiddle.com/#!15/133a9/7/0

select to_char(lookup_a.lookup_month, 'yyyy-mm'),
    count (distinct lookup_a.id) id_ct,
    count (distinct lookup_b.id) continuous_ct
from lookup lookup_a
left join lookup lookup_b 
    on lookup_b.id = lookup_a.id
    and lookup_b.lookup_month between lookup_a.lookup_month - interval '2 month'
    and lookup_a.lookup_month - interval '1 month'
group by 1  
order by 1
© www.soinside.com 2019 - 2024. All rights reserved.