sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ) as daily_costs
prev_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
        group by billing_cost_date
    ) as daily_costs
sum_avc_inc_percent: >
    select ((week2_avg_cost - week1_avg_cost) / week1_avg_cost) * 100 as increased_percentage from (
        select (
            select avg(daily_cost) from (
                select billing_cost_date, sum(billing_cost_price) as daily_cost 
                from dws.dbaas_billing.dedicated_customer_daily_cost 
                where billing_cost_date between date '{begin_date}' and date '{end_date}'
                group by billing_cost_date
            ) as week2_costs
        ) as week2_avg_cost, (
            select avg(daily_cost) from (
                select billing_cost_date, sum(billing_cost_price) as daily_cost 
                from dws.dbaas_billing.dedicated_customer_daily_cost 
                where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
                group by billing_cost_date
            ) as week1_costs
        ) as week1_avg_cost
    ) as weekly_avg_costs
max_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_cost_date
    order by sum(billing_cost_price) desc
    limit 1
max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
prev_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
    group by billing_cost_date
    order by daily_cost desc
    limit 1
min_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_cost_date
    order by sum(billing_cost_price)
    limit 1
min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_cost_date
    order by daily_cost
    limit 1
prev_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
    group by billing_cost_date
    order by daily_cost
    limit 1
sum_cost_every_day: >
    select billing_cost_date, format('%,.2f', sum(billing_cost_price)) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_cost_date
    order by billing_cost_date
paid_tenant_count: >
    select count(distinct billing_tenant_id) as tenant_count 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
paid_tenant_inc_count: >
    select (
        select count(distinct billing_tenant_id) 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
    ) - (
        select count(distinct billing_tenant_id) 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
    ) as tenant_increased
leave_paid_customers: >
    select tenant_name from dws.dbaas_billing.dedicated_customers where tenant_id in (
        select distinct billing_tenant_id 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
    ) and tenant_id not in (
        select distinct billing_tenant_id 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
    )
new_paid_customers: >
    select tenant_name from dws.dbaas_billing.dedicated_customers where tenant_id not in (
        select distinct billing_tenant_id 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
    ) and tenant_id in (
        select distinct billing_tenant_id 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
    )
biggest_customer_id: >
    select billing_tenant_id 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_tenant_id 
    order by sum(billing_cost_price) desc 
    limit 1
customer_name: >
    select tenant_name from dws.dbaas_billing.dedicated_customers where tenant_id = '{tenant_id}'
customer_daily_avg_percent: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ) select avg(tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100) as avg_percent 
      from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
      where billing_tenant_id = '{tenant_id}'
customer_daily_percent_diff: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ), daily_percent as (
        select tenant_daily_cost.billing_tenant_id, tenant_daily_cost.billing_cost_date, tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100 as percent 
        from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
        where billing_tenant_id = '{tenant_id}'
    ) select 
        (select percent from daily_percent order by billing_cost_date desc limit 1) 
        - 
        (select percent from daily_percent order by billing_cost_date limit 1) 
        as percent_diff
customer_max_daily_percent_date: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ), daily_percent as (
        select tenant_daily_cost.billing_tenant_id, tenant_daily_cost.billing_cost_date, tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100 as percent 
        from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
        where billing_tenant_id = '{tenant_id}'
    ) select billing_cost_date from daily_percent order by percent desc limit 1
customer_max_daily_percent_value: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ), daily_percent as (
        select tenant_daily_cost.billing_tenant_id, tenant_daily_cost.billing_cost_date, tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100 as percent 
        from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
        where billing_tenant_id = '{tenant_id}'
    ) select max(percent) as max_daily_percent from daily_percent
customer_min_daily_percent_date: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ), daily_percent as (
        select tenant_daily_cost.billing_tenant_id, tenant_daily_cost.billing_cost_date, tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100 as percent 
        from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
        where billing_tenant_id = '{tenant_id}'
    ) select billing_cost_date from daily_percent order by percent limit 1
customer_min_daily_percent_value: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ), daily_percent as (
        select tenant_daily_cost.billing_tenant_id, tenant_daily_cost.billing_cost_date, tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100 as percent 
        from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
        where billing_tenant_id = '{tenant_id}'
    ) select min(percent) as max_daily_percent from daily_percent
potential_customer_id: >
    with tenant_daily_cost as (
        select billing_tenant_id, billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id, billing_cost_date
    ), daily_cost as (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_cost_date
    ), daily_percent as (
        select tenant_daily_cost.billing_tenant_id, tenant_daily_cost.billing_cost_date, tenant_daily_cost.daily_cost / daily_cost.daily_cost * 100 as percent 
        from tenant_daily_cost join daily_cost on tenant_daily_cost.billing_cost_date = daily_cost.billing_cost_date 
    ), percent_diff as (
        select billing_tenant_id, percent - lag(percent) over (partition by billing_tenant_id order by billing_cost_date) as diff from daily_percent
    ) select billing_tenant_id from percent_diff group by billing_tenant_id order by sum(diff) desc limit 1
new_custermers: >
    select tenant_id, date(tenant_created_at) as tenant_created_at 
    from dws.dbaas_billing.dedicated_customers 
    where tenant_created_at > current_date - interval '180' day
    order by tenant_created_at desc
customer_sum_percent: >
    with billing as (
        select billing_tenant_id, sum(billing_cost_price) as tenant_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by billing_tenant_id
    )
    select
        (select coalesce(sum(tenant_cost), 0) from billing where billing_tenant_id = '{tenant_id}') * 100
        /
        (select sum(tenant_cost) from billing)
customer_prev_sum_percent: >
    with billing as (
        select billing_tenant_id, sum(billing_cost_price) as tenant_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day
        group by billing_tenant_id
    )
    select 
        (select coalesce(sum(tenant_cost), 0) from billing where billing_tenant_id = '{tenant_id}') * 100
        /
        (select sum(tenant_cost) from billing)
top_5_customers: >
    select billing_tenant_id 
    from dws.dbaas_billing.dedicated_customer_daily_cost
    where billing_cost_date between date '{begin_date}' and date '{end_date}'
    group by billing_tenant_id 
    order by sum(billing_cost_price) desc
    limit 5
customer_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}'
        group by billing_cost_date
    ) as daily_costs
prev_customer_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day and billing_tenant_id = '{tenant_id}'
        group by billing_cost_date
    ) as daily_costs
customer_max_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by sum(billing_cost_price) desc
    limit 1
customer_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
prev_customer_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
customer_min_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by sum(billing_cost_price)
    limit 1
customer_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by daily_cost
    limit 1
prev_customer_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by daily_cost
    limit 1
customer_sum_cost_every_day: >
    select billing_cost_date, format('%,.2f', sum(billing_cost_price)) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}'
    group by billing_cost_date
    order by billing_cost_date
customer_billing_project_count: >
    select count(distinct billing_project_id)
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_tenant_id = '{tenant_id}'
        and billing_project_id is not null
customer_prev_billing_project_count: >
    select count(distinct billing_project_id)
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
        and billing_tenant_id = '{tenant_id}'
        and billing_project_id is not null
customer_missing_projects: >
    select distinct(billing_project_name) as billing_project_name
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_project_id not in (
        select billing_project_id
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_cost_date between date '{begin_date}' and date '{end_date}' 
            and billing_tenant_id = '{tenant_id}'
            and billing_project_id is not null
    ) and billing_project_id in (
        select billing_project_id
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
            and billing_tenant_id = '{tenant_id}'
            and billing_project_id is not null
    )
customer_top_3_projects: >
    select billing_project_id, any_value(billing_project_name) as billing_project_name
    from dws.dbaas_billing.dedicated_customer_daily_cost
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}' and billing_project_id is not null
    group by billing_project_id
    order by sum(billing_cost_price) desc
    limit 3
project_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}'
        group by billing_cost_date
    ) as daily_costs
project_prev_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day and billing_project_id = '{project_id}'
        group by billing_cost_date
    ) as daily_costs
project_max_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by sum(billing_cost_price) desc
    limit 1
project_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
prev_project_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
project_min_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by sum(billing_cost_price)
    limit 1
project_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by daily_cost
    limit 1
prev_project_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by daily_cost
    limit 1
project_sum_cost_every_day: >
    select billing_cost_date, format('%,.2f', sum(billing_cost_price)) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}'
    group by billing_cost_date
    order by billing_cost_date
customer_new_projects: >
    with projects as (
        select billing_project_id, billing_project_name, billing_cost_date, row_number() over (partition by billing_project_id order by billing_cost_date) as rank
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_tenant_id = '{tenant_id}' and billing_project_id is not null
    )
    select billing_project_id, billing_project_name, billing_cost_date
    from projects
    where rank = 1 and billing_cost_date between date '{begin_date}' and date '{end_date}'
customer_disabled_projects: >
    with projects as (
        select billing_project_id, billing_project_name, billing_cost_date, row_number() over (partition by billing_project_id order by billing_cost_date desc) as rank
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_tenant_id = '{tenant_id}' and billing_project_id is not null
    )
    select billing_project_id, billing_project_name, billing_cost_date + interval '1' day as disabled_date
    from projects
    where rank = 1 and billing_cost_date between date '{begin_date}' - interval '1' day and date '{end_date}' - interval '1' day
customer_top_3_service: >
    select billing_unit
    from dws.dbaas_billing.dedicated_customer_daily_cost
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_tenant_id = '{tenant_id}' and billing_unit not like 'support-plan:%'
    group by billing_unit
    order by sum(billing_cost_price) desc
    limit 3
customer_unit_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}' 
            and billing_tenant_id = '{tenant_id}' 
            and billing_unit = '{unit}'
        group by billing_cost_date
    ) as daily_costs
prev_customer_unit_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
            and billing_tenant_id = '{tenant_id}' 
            and billing_unit = '{unit}'
        group by billing_cost_date
    ) as daily_costs
customer_unit_max_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' 
        and date '{end_date}' 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by sum(billing_cost_price) desc
    limit 1
customer_unit_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
prev_customer_unit_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
customer_unit_min_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by sum(billing_cost_price)
    limit 1
customer_unit_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by daily_cost
    limit 1
prev_customer_unit_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by daily_cost
    limit 1
customer_unit_sum_cost_every_day: >
    select billing_cost_date, format('%,.2f', sum(billing_cost_price)) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_tenant_id = '{tenant_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by billing_cost_date
customer_new_units: >
    with units as (
        select 
            billing_unit, 
            billing_cost_date, 
            sum(billing_cost_price) over (partition by billing_unit) as unit_cost,
            row_number() over (partition by billing_unit order by billing_cost_date) as rank
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_tenant_id = '{tenant_id}' and billing_unit not like 'support-plan:%'
    )
    select billing_unit, billing_cost_date, unit_cost
    from units
    where rank = 1 and billing_cost_date between date '{begin_date}' and date '{end_date}'
    order by unit_cost desc
customer_disabled_units: >
    with units as (
        select billing_unit, billing_cost_date, row_number() over (partition by billing_unit order by billing_cost_date desc) as rank
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_tenant_id = '{tenant_id}' and billing_unit not like 'support-plan:%'
    )
    select billing_unit, billing_cost_date + interval '1' day as disabled_date
    from units
    where rank = 1 and billing_cost_date between date '{begin_date}' - interval '1' day and date '{end_date}' - interval '1' day
project_top_3_service: >
    select billing_unit
    from dws.dbaas_billing.dedicated_customer_daily_cost
    where billing_cost_date between date '{begin_date}' and date '{end_date}' and billing_project_id = '{project_id}' and billing_unit not like 'support-plan:%'
    group by billing_unit
    order by sum(billing_cost_price) desc
    limit 3
project_unit_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' and date '{end_date}' 
            and billing_project_id = '{project_id}' 
            and billing_unit = '{unit}'
        group by billing_cost_date
    ) as daily_costs
prev_project_unit_sum_avg: >
    select avg(daily_cost) as avg_cost from (
        select billing_cost_date, sum(billing_cost_price) as daily_cost 
        from dws.dbaas_billing.dedicated_customer_daily_cost 
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
            and billing_project_id = '{project_id}' 
            and billing_unit = '{unit}'
        group by billing_cost_date
    ) as daily_costs
project_unit_max_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' 
        and date '{end_date}' 
        and billing_project_id = '{project_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by sum(billing_cost_price) desc
    limit 1
project_unit_max_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_project_id = '{project_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by daily_cost desc
    limit 1
prev_project_unit_max_daily_cost_value: >
    select sum(billing_cost_price) filter (
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
        and billing_project_id = '{project_id}'
        and billing_unit = '{unit}'
    ) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    group by billing_cost_date
    order by daily_cost desc
    limit 1
project_unit_min_daily_cost_date: >
    select billing_cost_date
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_project_id = '{project_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by sum(billing_cost_price)
    limit 1
project_unit_min_daily_cost_value: >
    select sum(billing_cost_price) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    where billing_cost_date between date '{begin_date}' and date '{end_date}' 
        and billing_project_id = '{project_id}'
        and billing_unit = '{unit}'
    group by billing_cost_date
    order by daily_cost
    limit 1
prev_project_unit_min_daily_cost_value: >
    select sum(billing_cost_price) filter (
        where billing_cost_date between date '{begin_date}' - interval '7' day and date '{end_date}' - interval '7' day 
        and billing_project_id = '{project_id}'
        and billing_unit = '{unit}'
    ) as daily_cost 
    from dws.dbaas_billing.dedicated_customer_daily_cost 
    group by billing_cost_date
    order by daily_cost
    limit 1
project_new_units: >
    with units as (
        select
            billing_unit, 
            billing_cost_date, 
            sum(billing_cost_price) over (partition by billing_unit) as unit_cost,
            row_number() over (partition by billing_unit order by billing_cost_date) as rank
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_project_id = '{project_id}' and billing_unit not like 'support-plan:%'
    )
    select billing_unit, billing_cost_date, unit_cost
    from units
    where rank = 1 and billing_cost_date between date '{begin_date}' and date '{end_date}'
    order by unit_cost desc
project_disabled_units: >
    with units as (
        select billing_unit, billing_cost_date, row_number() over (partition by billing_unit order by billing_cost_date desc) as rank
        from dws.dbaas_billing.dedicated_customer_daily_cost
        where billing_project_id = '{project_id}' and billing_unit not like 'support-plan:%'
    )
    select billing_unit, billing_cost_date + interval '1' day as disabled_date
    from units
    where rank = 1 and billing_cost_date between date '{begin_date}' - interval '1' day and date '{end_date}' - interval '1' day
sum_cost_every_day_by_tenant: >
    WITH tenant_daily_cost AS (
        SELECT
            dws.dbaas_billing.dedicated_customers.tenant_id,
            dws.dbaas_billing.dedicated_customers.tenant_name,
            dws.dbaas_billing.dedicated_customer_daily_cost.billing_cost_date,
            SUM(dws.dbaas_billing.dedicated_customer_daily_cost.billing_cost_price) AS daily_cost
        FROM
            dws.dbaas_billing.dedicated_customers
            JOIN dws.dbaas_billing.dedicated_customer_daily_cost ON dws.dbaas_billing.dedicated_customers.tenant_id = dws.dbaas_billing.dedicated_customer_daily_cost.billing_tenant_id
        WHERE
            dws.dbaas_billing.dedicated_customer_daily_cost.billing_cost_date BETWEEN DATE '{begin_date}' AND DATE '{end_date}'
        GROUP BY
            dws.dbaas_billing.dedicated_customers.tenant_id,
            dws.dbaas_billing.dedicated_customers.tenant_name,
            dws.dbaas_billing.dedicated_customer_daily_cost.billing_cost_date
    ), total_daily_cost AS (
        SELECT
            billing_cost_date,
            SUM(billing_cost_price) AS total_cost
        FROM
            dws.dbaas_billing.dedicated_customer_daily_cost
        WHERE
            billing_cost_date BETWEEN DATE '{begin_date}' AND DATE '{end_date}'
        GROUP BY
            billing_cost_date
    )
    SELECT
        tenant_name,
        tenant_daily_cost.billing_cost_date,
        ROUND(tenant_daily_cost.daily_cost / tdc_total.total_cost, 4) AS percent
    FROM
        tenant_daily_cost
        JOIN total_daily_cost tdc_total ON tenant_daily_cost.billing_cost_date = tdc_total.billing_cost_date
    ORDER BY
        tenant_daily_cost.billing_cost_date,
        tenant_name
sum_topn_cost_percent_by_tenant: >
    with tenant_daily_cost as (
        select 
            dws.dbaas_billing.dedicated_customers.tenant_name,
            dws.dbaas_billing.dedicated_customer_daily_cost.billing_tenant_id,
            sum(dws.dbaas_billing.dedicated_customer_daily_cost.billing_cost_price) as total_daily_cost 
        from 
            dws.dbaas_billing.dedicated_customer_daily_cost
            inner join dws.dbaas_billing.dedicated_customers 
            on dws.dbaas_billing.dedicated_customer_daily_cost.billing_tenant_id = dws.dbaas_billing.dedicated_customers.tenant_id
        where 
            dws.dbaas_billing.dedicated_customer_daily_cost.billing_cost_date between date '{begin_date}' and date '{end_date}'
        group by 
            dws.dbaas_billing.dedicated_customers.tenant_name,
            dws.dbaas_billing.dedicated_customer_daily_cost.billing_tenant_id
    ), ranked_tenant_total_cost as (
        select 
            tenant_name,
            sum(total_daily_cost) as total_cost,
            row_number() over (order by sum(total_daily_cost) desc) as rnk
        from 
            tenant_daily_cost
        group by 
            tenant_name
    )
    select 
        tenant_name,
        total_cost / sum(total_cost) over () * 100 as percentage
    from 
        ranked_tenant_total_cost
    order by 
        total_cost desc,
        tenant_name
