with animals as(select cast(id asbigint)as id
from tbl1
wheredate='${date}'and scene=2andtype=0and create_time>=unix_timestamp(
date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),30),'yyyy-MM-dd'))select animal_id,count(1)as num
from(selectdistinct
animal_id,
create_time
from tbl2
wheredate='${date}'and id in(select id
from animals
)groupby
animal_id,
create_time
)groupby
animal_id
havingcount(1)>1
优化后的
with animals as(select cast(id asbigint)as id
from tbl1
wheredate='${date}'and scene=2andtype=0and create_time>=unix_timestamp(
date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),30),'yyyy-MM-dd'))select animal_id,COUNT(distinct create_time)as num
from tbl2
wheredate='${date}'and id in(select id
from animals
)groupby
animal_id
havingCOUNT(distinct create_time)>1