Sql 如何计算新id的数量';s在每个月出现(id在前几个月不应出现)
我正试图获得每个月的id的计数,这些id在下个月不存在。 下面是我目前编写的sql,但看起来不是一个好方法。有没有更好的方法可以做到这一点?查询将有几个月的时间Sql 如何计算新id的数量';s在每个月出现(id在前几个月不应出现),sql,hive,Sql,Hive,我正试图获得每个月的id的计数,这些id在下个月不存在。 下面是我目前编写的sql,但看起来不是一个好方法。有没有更好的方法可以做到这一点?查询将有几个月的时间 select count(distinct id) from table1 where bill_yearmo='202007' and id not in (select acct_id from table1 where bill_yearmo='202006' ) 您可以使用两个级别的聚合: sele
select count(distinct id) from table1
where bill_yearmo='202007' and id not in (select
acct_id from table1
where bill_yearmo='202006' )
您可以使用两个级别的聚合:
select first_bill_yearmo, count(*) as num_starts
from (select id, min(bill_yearmo) as first_bill_yearmo
from table1
group by id
) i
group by first_bill_yearmo;