select t.MasterID,t.AssortId,t.id,t.PDF_PATH,t.Source,CONVERT(varchar(100),t.UpLoadDateTime, 120) UpLoadDateTime,t.SubAssort,t.Title,t.flag,t.page_number from archive_detail t left join archive_master f on t.MasterID=f.id
where 1=1
<iftest="patientId != null">
and f.id=#{patientId,jdbcType=VARCHAR}
</if>
and t.Source is not null) d
on z.assort_id=d.AssortId
where d.Source is not null
<iftest="assortId != null and assortId != ''">
and d.AssortID=#{assortId,jdbcType=VARCHAR}
</if>
<iftest="source != null and source != '' and source!='-1'">
and d.Source=#{source,jdbcType=VARCHAR}
</if>
<iftest="source =='-1'">
and d.Source is null
</if>
<iftest="flag != null and flag != ''">
and d.flag = #{flag,jdbcType=NVARCHAR}
</if>
<iftest="title != null and title != ''">
and d.title like '%'+#{title,jdbcType=VARCHAR}+'%'
s.T2,s.T3,s.IsFullText,m.inp_no,m.visit_id,m.name,d.name as deptName,m.discharge_date_time,m.isscaned,(select COUNT(*) from archive_other where archive_other.pid=s.pid)as num
s.T2,s.T3,s.IsFullText,m.inp_no,m.visit_id,m.name,CONVERT(varchar(10),m.admission_date_time, 120) admissionDateTime,d.name as deptAdmissionTo,m.isscaned,(select COUNT(*) from archive_other where archive_other.pid=s.pid)as num
from archive_other s
left join archive_master m
on s.masterID=m.id
LEFT JOIN emr_dictionary d
on m.dept_admission_to=d.code
LEFT JOIN (select code,name from emr_dictionary where parent_id=(select id from emr_dictionary where
typeCode='dept_code' )) f
on m.dept_name=f.code
where s.id in(
(select top 1 id from archive_other a where a.pid=s.pid and a.masterID=s.masterID ORDER BY createdtime desc)