Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何计算新id的数量';s在每个月出现(id在前几个月不应出现)_Sql_Hive - Fatal编程技术网

Sql 如何计算新id的数量';s在每个月出现(id在前几个月不应出现)

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

我正试图获得每个月的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' )

您可以使用两个级别的聚合:

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;