简体   繁体   中英

How to avoid using aggregation function in another aggregation function in ClickHouse

I want to get the average number of days between creation user profile ads_clients_data.create_date and the first day of its posts min(ads_data.time) .

I wrote the following SQL statement:

select 
    avg(dateDiff(dd, ads_clients_data.create_date, ads_data.time),
    min(ads_data.time))
from 
    ads_data
inner join 
    ads_clients_data on ads_clients_data.client_union_id = ads_data.client_union_id;

But it is impossible to use min in avg function. It is the first time I worked with ClickHouse and would be very thankful if someone can help me.

If I follow you correctly, you can use two levels of aggregation:

select avg(datediff(dd, c.create_date, d.min_time)
from (
    select client_union_id, min(ads_data.time) min_time
    from ads_data
    group by client_union_id
) d
inner join ads_clients_data c on c.client_union_id = d.client_union_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM