select c.visit_id, c.visit_type, c.py,c.wb, c.unit_id, (select p.unit_name from sys_unit p where p.unit_id =c.unit_id) as UnitName, c.name, c.INPATIENT_NO, c.ENTER_DEPT, c.visit_times, s.dept_name as ENTER_DEPT_Name, c.ENTER_TIME, c.Bed_Text, w.ward_id, w.ward_name, c.card_no, c.card_type, c.current_dept, s1.DEPT_NAME as Current_Dept_Name , c.RECEPT_MARK, h.name as accountname, k.user_name as RESIDENTNAME, (select m1.diag_name from mrs_doc_diagnosis m1 where m1.VISIT_ID = c.visit_id and rownum<2) as diagname, c.out_time, c.IN_AMOUNT, c.PRE_AMOUNT from CIS_INPATIENT c left join sys_dept s on c.ENTER_DEPT = s.dept_id left join sys_ward w on c.ward_id=w.ward_id and w.unit_id=c.unit_id left join sys_dept s1 on c.CURRENT_DEPT = s1.dept_id left join dict_account_type h on c.account_type = h.code left join sys_user@sj_sys k on k.user_id= c.RESIDENT_ID where ( c.mid_bespoke_time is not null or c.bespoke_time is not null ) and c.account_time is null order by out_time DESC