日期:2014-05-17 浏览次数:20909 次
with tab as( select ttt.*,tt.cnum from ( select '111' id, 'aaa' name, '代理' type,1 order_id from dual union all select '111', 'aaa', '其他' ,2 from dual union all select '操作员小计', '', '18',3 from dual union all select '商户小计', '', '22' ,4 from dual union all select '222', 'bbb', '其他' ,5 from dual union all select '222', 'bbb', '代理' , 6 from dual union all select '操作员小计', '', '99' ,7 from dual union all select '商户小计', '', '98' , 8 from dual)ttt left join (select '2' as cnum,'aaa' as name from dual ) tt on ttt.name=tt.name ORDER BY order_id ) select id ,name,type,rn1 from ( select id,name ,type ,row_number()over(partition by id,name order by rownum) rn ,rownum rn1 from tab) order by rn1;
------解决方案--------------------
with tab as(
select ttt.*,tt.cnum from (
select id,name,type,rownum r1 from(
select '111' id, 'aaa' name, '代理' type from dual union all
select '111', 'aaa', '其他' from dual union all
select '操作员小计', '', '18' from dual union all
select '商户小计', '', '22' from dual union all
select '222', 'bbb', '其他' from dual union all
select '222', 'bbb', '代理' from dual union all
select '操作员小计', '', '99' from dual union all
select '商户小计', '', '98' from dual))ttt
left join
(select '2' as cnum,'aaa' as name from dual ) tt
on ttt.name=tt.name order by ttt.r1
)
select id ,name,type,rn1 from (
select id,name ,type ,row_number()over(partition by id,name order by rownum) rn ,rownum rn1 from tab)
order by rn1;