3个用union all连接到一起的怎么变成子查询给他加上rownum让他排序?

select year_month,his_dept_code,his_dept_name,sum(in_dept_num) in_dept_num,sum(trans_dept_num) trans_dept_num,sum(on_dept_num) on_dept_num from(
select
to_char(a.INDATE,'yyyy-mm-dd') year_month,
(select deptcode from om_department where deptid=a.INDEPT) his_dept_code,
(select deptname from om_department where deptid=a.INDEPT) his_dept_name,
count(a.ENCOUNTERID) in_dept_num,
0 as trans_dept_num,
0 as on_dept_num
from
p_i_date_rester a
where
a.ISDELETE=0 and
to_char(a.indate,'yyyy.mm.dd')>='2014.01.06'
group by
to_char(a.INDATE,'yyyy-mm-dd'),
a.INDEPT

union all

select
to_char(a.outDATE,'yyyy-mm-dd') year_month,
(select deptcode from om_department where deptid=a.INDEPT) his_dept_code,
(select deptname from om_department where deptid=a.INDEPT) his_dept_name,
0 as in_dept_num,
count(a.ENCOUNTERID) trans_dept_num,
0 as on_dept_num
from
p_i_date_rester a
where
a.ISDELETE=0 and
to_char(a.indate,'yyyy.mm.dd')>='2014.01.06'
group by
to_char(a.outDATE,'yyyy-mm-dd'),
a.INDEPT

union all

select
to_char(a.billdate,'yyyy-mm-dd') year_month,
(select deptcode from om_department where deptid=b.INDEPT) his_dept_code,
(select deptname from om_department where deptid=b.INDEPT) his_dept_name,
0 as in_dept_num,
0 as trans_dept_num,
count(distinct a.encounterid) on_dept_sum
from
a_i_date_bil a,p_i_date_rester b
where
a.encounterid=b.encounterid and
b.isdelete=0
group by
to_char(a.billdate,'yyyy-mm-dd'),
b.INDEPT
)
group by year_month,his_dept_code,his_dept_name

外面再加一层,给它排序