Browse Source

sql优化,去除多余子查询

chengyao 3 năm trước cách đây
mục cha
commit
e42d08e30f
1 tập tin đã thay đổi với 39 bổ sung98 xóa
  1. 39 98
      src/main/resources/mapper/BehospitalInfoMapper.xml

+ 39 - 98
src/main/resources/mapper/BehospitalInfoMapper.xml

@@ -3377,16 +3377,15 @@
 
     <!-- 医师病案首页合格率占比 湘雅定制-->
     <select id="homePageLevelStatisticsXY" resultType="com.diagbot.dto.HomePageNumXYDTO">
-
         SELECT
-           t4.*,
+        t4.*,
+        bas.name as doctorName,
         ifnull(bas.professor,'')as professor
         FROM (
         SELECT
         t1.deptId,
         t1.deptName,
         t1.doctorId,
-        t1.doctorName,
         t2.entryNum,
         t1.mrNum,
         t1.totleValue,
@@ -3397,20 +3396,27 @@
         t1.secondLevelPercent,
         t1.firstLevelPercentStr,
         t1.secondLevelPercentStr,
-        t3.emptyNum,
-        t3.errorNum,
-        t3.entryTotleNum,
-        t3.emptyPercent,
-        t3.errorPercent,
-        t3.emptyPercentStr,
-        t3.errorPercentStr
+        t2.emptyNum,
+        t2.errorNum,
+        t2.mrNum * t3.entryNum AS entryTotleNum,
+        ROUND( ( t2.mrNum * t3.entryNum  - t2.emptyNum ) / ( t2.mrNum * t3.entryNum ), 4 ) AS emptyPercent,
+        ROUND( ( t2.mrNum * t3.entryNum  - t2.errorNum ) / ( t2.mrNum * t3.entryNum ), 4 ) AS errorPercent,
+        CONCAT( ROUND( ( t2.mrNum * t3.entryNum - t2.emptyNum ) / ( t2.mrNum * t3.entryNum )* 100, 2 ), '%' ) AS emptyPercentStr,
+        CONCAT( ROUND( ( t2.mrNum * t3.entryNum - t2.errorNum ) / ( t2.mrNum * t3.entryNum )* 100, 2 ), '%' ) AS errorPercentStr
         FROM
         (
         SELECT
         a.beh_dept_id AS deptId,
         a.beh_dept_name AS deptName,
-        a.doctor_id AS doctorId,
-        a.doctor_name AS doctorName,
+        case
+        when a.doctor_id != '' and a.doctor_id is not null then a.doctor_id
+        when (a.doctor_id = '' or a.doctor_id is null) and
+        a.director_doctor_id != '' and a.director_doctor_id is not null
+        then a.director_doctor_id
+        when (a.doctor_id = '' or a.doctor_id is null) and
+        (a.director_doctor_id = '' or a.director_doctor_id is null) and
+        a.beh_doctor_id != '' and a.beh_doctor_id is not null
+        then a.beh_doctor_id end as doctorId,
         count( * ) AS mrNum,
         ROUND( sum( CAST( c.score_res AS DECIMAL ( 18, 2 )) ), 2 ) AS totleValue,
         ROUND( avg( CAST( c.score_res AS DECIMAL ( 18, 2 )) ), 2 ) AS averageValue,
@@ -3462,14 +3468,26 @@
         a.beh_dept_id,
         a.beh_dept_name,
         a.doctor_id,
-        a.doctor_name
+        a.director_doctor_id,
+        a.beh_doctor_id
         ) t1,
         (
         SELECT
         a.beh_dept_id AS deptId,
         a.beh_dept_name AS deptName,
-        a.doctor_id AS doctorId,
-        count( * ) AS entryNum
+        case
+        when a.doctor_id != '' and a.doctor_id is not null then a.doctor_id
+        when (a.doctor_id = '' or a.doctor_id is null) and
+        a.director_doctor_id != '' and a.director_doctor_id is not null
+        then a.director_doctor_id
+        when (a.doctor_id = '' or a.doctor_id is null) and
+        (a.director_doctor_id = '' or a.director_doctor_id is null) and
+        a.beh_doctor_id != '' and a.beh_doctor_id is not null
+        then a.beh_doctor_id end as doctorId,
+        count( * ) AS entryNum,
+        count(case when e.rule_type = 1 then 1 end) AS emptyNum,
+        count(case when e.rule_type = 2 then 1 end) AS errorNum,
+        count( DISTINCT a.behospital_code ) AS mrNum
         FROM
         med_behospital_info a,
         med_qcresult_cases c,
@@ -3521,108 +3539,31 @@
         a.beh_dept_id,
         a.beh_dept_name,
         a.doctor_id,
-        a.doctor_name
+        a.director_doctor_id,
+        a.beh_doctor_id
         ) t2,(
         SELECT
-        h1.deptId,
-        h1.deptName,
-        h1.doctorId,
-        h1.emptyNum,
-        h1.errorNum,
-        h1.mrNum,
-        h2.entryNum,
-        h1.mrNum * h2.entryNum AS entryTotleNum,
-        ROUND( ( h1.mrNum * h2.entryNum  - h1.emptyNum ) / ( h1.mrNum * h2.entryNum ), 4 ) AS emptyPercent,
-        ROUND( ( h1.mrNum * h2.entryNum  - h1.errorNum ) / ( h1.mrNum * h2.entryNum ), 4 ) AS errorPercent,
-        CONCAT( ROUND( ( h1.mrNum * h2.entryNum - h1.emptyNum ) / ( h1.mrNum * h2.entryNum )* 100, 2 ), '%' ) AS emptyPercentStr,
-        CONCAT( ROUND( ( h1.mrNum * h2.entryNum - h1.errorNum ) / ( h1.mrNum * h2.entryNum )* 100, 2 ), '%' ) AS errorPercentStr
-        FROM
-        (
-        SELECT
-        a.beh_dept_id AS deptId,
-        a.beh_dept_name AS deptName,
-        a.doctor_id as doctorId,
-        count(case when d.rule_type = 1 then 1 end) AS emptyNum,
-        count(case when d.rule_type = 2 then 1 end) AS errorNum,
-        a.doctor_id as doctorId1,
-        count( DISTINCT a.behospital_code ) AS mrNum
-        FROM
-        med_behospital_info a,
-        med_qcresult_cases b,
-        med_qcresult_detail c,
-        qc_cases_entry d
-        WHERE
-        a.is_deleted = 'N'
-        AND b.is_deleted = 'N'
-        AND c.is_deleted = 'N'
-        AND d.is_deleted = 'N'
-        AND a.hospital_id = b.hospital_id
-        AND a.hospital_id = c.hospital_id
-        AND a.behospital_code = b.behospital_code
-        AND a.behospital_code = c.behospital_code
-        AND b.cases_id = c.cases_id
-        AND c.cases_id = d.cases_id
-        AND c.cases_entry_id = d.id
-        <if test="isPlacefile != null and isPlacefile != ''">
-            and a.is_placefile = #{isPlacefile}
-        </if>
-        AND d.cases_id = 243
-        AND a.qc_type_id != 0
-        <if test="hospitalId != null and hospitalId != ''">
-            AND a.hospital_id = #{hospitalId}
-        </if>
-        <if test="isPlacefile != null and isPlacefile == 0">
-            <if test="startDate != null and startDate != ''">
-                <![CDATA[ AND a.behospital_date >= #{startDate}]]>
-            </if>
-            <if test="endDate != null and endDate != ''">
-                <![CDATA[ AND a.behospital_date <= #{endDate}]]>
-            </if>
-        </if>
-        <if test="isPlacefile != null and isPlacefile == 1">
-            <if test="startDate != null and startDate != ''">
-                <![CDATA[ AND a.leave_hospital_date >= #{startDate}]]>
-            </if>
-            <if test="endDate != null and endDate != ''">
-                <![CDATA[ AND a.leave_hospital_date <= #{endDate}]]>
-            </if>
-        </if>
-        <if test="deptName != null and deptName != ''">
-            and a.beh_dept_name like CONCAT('%',#{deptName},'%')
-        </if>
-        <if test="doctorName != null and doctorName != ''">
-            and a.doctor_name like CONCAT('%',#{doctorName},'%')
-        </if>
-        GROUP BY
-        a.beh_dept_id,
-        a.beh_dept_name,
-        a.doctor_id,
-        a.doctor_name
-        ) h1,(
-        SELECT
         count(*) AS entryNum
         FROM
         qc_cases_entry
         WHERE
         is_deleted = 'N'
         AND cases_id = 243
-        ) h2
         ) t3
         WHERE
         t1.deptId = t2.deptId
-        AND t1.deptId = t3.deptId
         AND t1.deptName = t2.deptName
-        AND t1.deptName = t3.deptName
-        AND t1.doctorId = t2.doctorId
-        AND t1.doctorId = t3.doctorId)t4
+        AND t1.doctorId = t2.doctorId)t4
         left join bas_doctor_info bas
         on t4.doctorId = bas.doctor_id
         and bas.is_deleted = 'N'
         where
-        t4.doctorName != ''
+        t4.doctorId != ''
+        and t4.doctorId is not null
         <if test="professor != null and professor != ''">
             and bas.professor = #{professor}
         </if>
+        group by t4.doctorId
     </select>