round(sum(case when ii.name = '磷生铁中P' then ROUND(CAST(ii.test_value AS NUMERIC),5) else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁中P' then 1 else null end),1),2) "p_val",
round(sum(case when ii.name = '磷生铁中S' then ROUND(CAST(ii.test_value AS NUMERIC),5) else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁中S' then 1 else null end),1),2) "s_val"
FROM
entrust_sample_item i,
entrust_sample_item_index ii
WHERE
i.ID = ii.entrust_sample_item_id
AND i.deleted = 0
AND ii.deleted = 0
entrust_sample_item i
JOIN entrust_sample_item_index ii ON ii.entrust_sample_item_id = i.id and ii.deleted = 0
JOIN entrust_sample s on s.id = i.entrust_sample_id and s.deleted = 0
WHERE i.deleted = 0
AND s.name like '%磷生铁%'
AND i.period is not null
AND ii.test_value is not null and is_numeric(ii.test_value)
AND EXISTS (
select 1 from entrust_sample s where s.id = i.entrust_sample_id and s.deleted = 0
and s.name like '%磷生铁%'
)
<iftest="null != vo.timeS and null != vo.timeE">
and to_char(i.test_time, 'YYYY-MM-dd HH24:MI:SS') BETWEEN #{vo.timeS} AND #{vo.timeE}
</if>
<iftest="null != vo.timeS and null != vo.timeE">
and date(i.test_time) >= date(#{vo.timeS}) AND date(i.test_time) <= date(#{vo.timeE})
</if>
GROUP BY i.company_id,i.period,i.entrust_sample_id