SELECT k.ICD10_TM , k.NICKNAME, COUNT(k.ICD10_TM) as AMOUNT
FROM (
SELECT i.VISIT_ID ,i.ADM_DT, r.RF_DT ,
((to_days(r.RF_DT)*24)- (to_days(i.ADM_DT)*24))/24 AS DAYS, abs((time_to_sec(r.RF_DT)/3600) - (time_to_sec(i.ADM_DT)/3600)) as Times
,ic.ICD10_TM , ic.NICKNAME
FROM ipd_reg i
INNER JOIN refers r on i.VISIT_ID = r.VISIT_ID AND i.IS_CANCEL = 0 AND r.IS_CANCEL = 0 AND r.rf_type = 2
INNER JOIN opd_diagnosis o ON i.VISIT_ID = o.VISIT_ID AND o.IS_CANCEL = 0 AND o.DXT_ID = 1
INNER JOIN icd10new ic ON o.ICD10 = ic.ICD10
WHERE r.RF_DT BETWEEN '2018-10-01 00:00' AND '2019-09-30 23:59'
AND ((to_days(r.RF_DT)*24)- (to_days(i.ADM_DT)*24))/24 = '0'
AND abs((time_to_sec(r.RF_DT)/3600) - (time_to_sec(i.ADM_DT)/3600)) <= '4.0') as k
GROUP BY k.ICD10_TM ORDER BY AMOUNT DESC
****List แยก ตามAN***
SELECT i.VISIT_ID ,op.HN,i.ADM_ID as AN ,op.REG_DATETIME as REGDATE,i.ADM_DT, r.RF_DT ,
((to_days(r.RF_DT)*24)- (to_days(i.ADM_DT)*24))/24 AS DAYS, abs((time_to_sec(r.RF_DT)/3600) - (time_to_sec(i.ADM_DT)/3600)) as Times
,ic.ICD10_TM , ic.NICKNAME
FROM ipd_reg i
LEFT JOIN opd_visits op ON op.visit_id = i.visit_id AND op.is_cancel = 0
INNER JOIN refers r on i.VISIT_ID = r.VISIT_ID AND i.IS_CANCEL = 0 AND r.IS_CANCEL = 0 AND r.rf_type = 2
INNER JOIN opd_diagnosis o ON i.VISIT_ID = o.VISIT_ID AND o.IS_CANCEL = 0 AND o.DXT_ID = 1
INNER JOIN icd10new ic ON o.ICD10 = ic.ICD10
WHERE r.RF_DT BETWEEN '2020-01-01 00:00' AND '2021-09-30 23:59'
AND ((to_days(r.RF_DT)*24)- (to_days(i.ADM_DT)*24))/24 = '0'
AND abs((time_to_sec(r.RF_DT)/3600) - (time_to_sec(i.ADM_DT)/3600)) <= '2.0'
#GROUP BY k.ICD10_TM ORDER BY AMOUNT DESC