with all_data as (
select
creationDate,
Average,
Homogen,
difference,
net_weight,
jsonb_array_elements(testData::jsonb) ->> 'MaxPower' AS maxpower,
jsonb_array_elements(testData::jsonb) ->> 'Thickness' AS thickness,
jsonb_array_elements(testData::jsonb) ->> 'Refraction' AS refraction,
FurnaceID,
equipmentNo as FurnaceID,
lineNo as FurnaceID,
CONCAT(RIGHT(FurnaceID, 2), '-', pipeNo) as luguanhao,
CONCAT(equipmentNo, '-', pipeNo) as luguanhao,
lineNo as luguanhao,
LEFT(pipeNo, LENGTH(pipeNo) -1) as num,
case
when to_char(creationDate, 'HH24')::INTEGER >= 8 and to_char(creationDate, 'HH24')::INTEGER < 20 then 'D'
else 'N'
end as banci
FROM
battery_spc_gather_auto
spc_anneal_offline
spc_anneal_offline
spc_oxidation_offline
battert_spc_scales_texturing
battert_spc_scales_alkalicase
spc_texturing_offline
spc_alkalicase_offline
spc_pepoly_offline
spc_alumina1_offline
spc_alumina2_offline
spc_positive_membrane_offline
spc_dorsal_membrane_offline
spc_pullingforce1_offline
spc_pullingforce2_offline
spc_threepointbendingresistance_offline
battert_spc_scales_silk
where 2=2
and ${reportCombox} = 2
and workshop = 'C1'
and creationDate >= to_timestamp('${startDate}', 'YYYY-MM-DD HH24:MI:SS')
and creationDate < to_timestamp('${endDate}', 'YYYY-MM-DD HH24:MI:SS')
${if(len(deviceId) > 0," and FurnaceID in ('" +deviceId+ "') ","")}
${if(len(deviceId) > 0," and equipmentNo in ('" +deviceId+ "') ","")}
${if(len(deviceId) > 0," and lineNo in ('" +deviceId+ "') ","")}
)
select
luguanhao,
AVG(cast(Average as DECIMAL)) as Average,
AVG(cast(Homogen as DECIMAL)) as Homogen
AVG(cast(difference as DECIMAL)) as difference
AVG(cast(net_weight as DECIMAL)) as netWeight
AVG(cast(maxpower as DECIMAL)) as maxpower
AVG(cast(thickness as DECIMAL)) as thickness
AVG(cast(refraction as DECIMAL)) as refraction
from all_data ad
where 1=1
${if(len(banci)==0,"","and ad.banci = '"+banci+"'")}
group by luguanhao
order by RIGHT(ad.FurnaceID, 2)::DECIMAL, ad.num
group by luguanhao
order by ad.FurnaceID::DECIMAL, ad.num
group by luguanhao
order by ad.FurnaceID::DECIMAL
;