and cps.name like concat('%', #{params.name}, '%')
and cps.region_id = #{params.regionId}::bigint
and temp.product_type like concat('%', #{params.productType}, '%')
select count(cps.*)
from carbon_pub_supplier cps
left join (select string_agg(distinct industry_sub, ',') as product_type, tenant_code
from iot_carbon_production_variety
group by tenant_code) temp on cps.code = temp.tenant_code
select id, name, code, access_token, region_id, points, temp.product_type, t2.records_count
from carbon_pub_supplier cps
left join (select string_agg(distinct industry_sub, ',') as product_type, tenant_code
from iot_carbon_production_variety
group by tenant_code) temp on cps.code = temp.tenant_code
left join (select sum(pr_unit) as records_count, tenant_code
from (select count(distinct pr_code) as pr_unit, tenant_code, pr_code
from carbon_pub_production_result
group by product_id, pr_code, tenant_code) t1
group by tenant_code) t2 on cps.code = t2.tenant_code
limit #{params.limit} offset #{params.offset}
select COALESCE( avg(carbon), 0 ) AS carbon
from (select tenant_code,
sum(type1_carbon_avg)
+ sum(type1_usage_avg)
+ sum(type2_carbon_avg)
+ sum(type3_carbon_avg)
+ sum(type4_carbon_avg)
+ sum(type5_carbon_avg)
+ sum(type6_carbon_avg)
+ sum(type6_fq_carbon_avg) as carbon
from (select product_id,
product_name,
pr_code,
carbon_type,
tenant_code,
case carbon_type when '1' then carbon_avg else 0 end as type1_carbon_avg,
case carbon_type when '1' then usage_avg else 0 end as type1_usage_avg,
case carbon_type when '2' then carbon_avg else 0 end as type2_carbon_avg,
case carbon_type when '3' then carbon_avg else 0 end as type3_carbon_avg,
case carbon_type when '4' then carbon_avg else 0 end as type4_carbon_avg,
case carbon_type when '5' then carbon_avg else 0 end as type5_carbon_avg,
case carbon_type when '6' then carbon_avg else 0 end as type6_carbon_avg,
case carbon_type when '6' then fq_carbon_avg else 0 end as type6_fq_carbon_avg
from (select product_id,
product_name,
pr_code,
carbon_type,
tenant_code,
sum(carbon_avg) as carbon_avg,
sum(usage_avg) as usage_avg,
sum(fq_carbon_avg) as fq_carbon_avg
from carbon_pub_production_result
and tenant_code = #{tenantCode}
and finish_time >= #{start}
and finish_time <= #{end}
group by product_id, product_name, pr_code, carbon_type, tenant_code) as t1) as t2
group by product_id, product_name, pr_code, tenant_code) as t3
where tenant_code = #{tenantCode}