ProjectData_WorkArea workArea on workArea.UnitId=unit.UnitId
left join
( SELECT WorkAreaId,IsoInfo.UnitId,COUNT(*) AS curr_check_count_total
, SUM(cht_totalfilm) AS curr_total_film
, SUM(cht_passfilm) AS curr_pass_film
FROM CH_Check Checks
LEFT JOIN CH_CheckItem ON ch_checkitem.CHT_CheckID = Checks.CHT_CheckID
LEFT JOIN PW_JointInfo as JointInfo ON JointInfo.JOT_ID = ch_checkitem.JOT_ID
LEFT JOIN PW_IsoInfo as IsoInfo ON IsoInfo.ISO_ID = JointInfo.ISO_ID
where JointInfo.ProjectId=@ProjectId and (cht_checkdate >= @date1 OR @date1 IS NULL) and(cht_checkdate <= @date2 OR @date2 IS NULL)
group by WorkAreaId,IsoInfo.UnitId) as curr on unit.UnitId= curr.UnitId and workArea.WorkAreaId=curr.WorkAreaId
left join
( SELECT WorkAreaId,IsoInfo.UnitId,COUNT(*) AS recurr_check_count_total
, SUM(cht_totalfilm) AS recurr_total_film
, SUM(cht_passfilm) AS recurr_pass_film
FROM CH_Check Checks
LEFT JOIN CH_CheckItem ON ch_checkitem.CHT_CheckID = Checks.CHT_CheckID
LEFT JOIN PW_JointInfo as JointInfo ON JointInfo.JOT_ID = ch_checkitem.JOT_ID
LEFT JOIN PW_IsoInfo as IsoInfo ON IsoInfo.ISO_ID = JointInfo.ISO_ID
LEFT JOIN CH_Trust trust on trust.CH_TrustID = Checks.CH_TrustID
where JointInfo.ProjectId=@ProjectId and trust.CH_TrustType='2' and (cht_checkdate >= @date1 OR @date1 IS NULL) and(cht_checkdate <= @date2 OR @date2 IS NULL)
group by WorkAreaId,IsoInfo.UnitId) as recurr on unit.UnitId= recurr.UnitId and workArea.WorkAreaId=recurr.WorkAreaId
left join
( SELECT WorkAreaId,IsoInfo.UnitId,COUNT(*) AS curr_point_count_total
, SUM(case when CHT_CheckResult='合格' then 1 else 0 end) AS curr_pass_point
FROM CH_Check Checks
LEFT JOIN CH_CheckItem ON ch_checkitem.CHT_CheckID = Checks.CHT_CheckID
LEFT JOIN PW_JointInfo as JointInfo ON JointInfo.JOT_ID = ch_checkitem.JOT_ID
LEFT JOIN PW_IsoInfo as IsoInfo ON IsoInfo.ISO_ID = JointInfo.ISO_ID
where JointInfo.ProjectId=@ProjectId and (cht_checkdate >= @date1 OR @date1 IS NULL) and(cht_checkdate <= @date2 OR @date2 IS NULL)
group by WorkAreaId,IsoInfo.UnitId) as currpoint on unit.UnitId= currpoint.UnitId and workArea.WorkAreaId=currpoint.WorkAreaId
left join (
SELECT WorkAreaId,IsoInfo.UnitId,COUNT(*) AS curr_trust_count_total
FROM CH_Trust trust
LEFT JOIN CH_TrustItem trustitem ON trustitem .CH_TrustID = trust.CH_TrustID
LEFT JOIN PW_JointInfo as JointInfo ON JointInfo.JOT_ID = trustitem.JOT_ID
LEFT JOIN PW_IsoInfo as IsoInfo ON IsoInfo.ISO_ID = JointInfo.ISO_ID
where JointInfo.ProjectId=@ProjectId and (trust.CH_TrustDate >= @date1 OR @date1 IS NULL) and(trust.CH_TrustDate <= @date2 OR @date2 IS NULL)
group by WorkAreaId,IsoInfo.UnitId) as curr_trust_point on unit.UnitId= curr_trust_point.UnitId and workArea.WorkAreaId=curr_trust_point.WorkAreaId