Oracle 분할 합병 표 정리

Oracle 분할 합병 표
/****************************************************************************************
Oracle     

   :   (Roy)
  :2011.11.02
*****************************************************************************************/

/**--  

--    
Col1	Col2
1	a
1	b
1	c
2	d
2	e
3	f
**/

/**--    

COL1 	COL2
1 	a,b,c
2 	d,e
3 	f 
**/


/**oracle10g         wmsys.wm_concat**/

/**  1**/
with Tab
as
(
select 1 as Col1,'a'  as Col2 from dual union all
select 1,'b' from dual  union all
select 1,'c' from dual union all
select 2,'d' from dual union all
select 2,'e' from dual union all
select 3,'f' from dual 
)
select
	Col1,wmsys.wm_concat(Col2 ) as Col2
from tab  group by Col1


/**oracle9i   connect by**/

/**  2**/
with Tab
as
(
select 1 as Col1,'a'  as Col2 from dual union all
select 1,'b' from dual  union all
select 1,'c' from dual union all
select 2,'d' from dual union all
select 2,'e' from dual union all
select 3,'f' from dual 
)
select 
	Col1,substr(max(sys_connect_by_path(Col2,',')),2) Col2
from 
	(select a.*,row_number()over(partition by Col1 order by Col1) rn from Tab a )
group by Col1 start with rn=1
connect by rn-1=prior rn and Col1=prior Col1
order by Col1;

/**--  

--    
Col1	Col2
1	a,b,c
2	d,e
3	f
**/

/**--    

COL1 	COL2
1 	a
1 	b
1 	c
2 	d
2 	e
3 	f 
**/
/**  1**/
with Tab
as
(select 1 as Col1,N'a,b,c' as Col2  from dual union all
select 2,N'd,e' from dual union all
select 3,N'f'   from dual )
SELECT 
      Col1,substr(Col2,lev,instr(Col2||',',',',lev)-lev) as Col2
from Tab 
	,(SELECT LEVEL lev FROM DUAL CONNECT BY LEVEL<=100)
WHERE 
	substr(','||Col2,lev,1)=',' /**       instr(','||Col2,',',lev)=lev**/
order by Col1

/**  2
REGEXP_SUBSTR(srcstr, pattern, position, occurrence, modifier)  
__srcstr        :     
__pattern      :    
__position     :  srcstr     (   1)
__occurrence:               (   1)
__modifier     :    ('i'          ;'c'         。   'c'。)  
**/

with Tab
as
(select 1 as Col1,N'a,b,c' as Col2  from dual union all
select 2,N'd,e' from dual union all
select 3,N'f'   from dual )
SELECT 
	Col1,REGEXP_SUBSTR(Col2,'[^,]+',1,lev)
FROM Tab,
	(SELECT LEVEL lev FROM dual CONNECT BY LEVEL <= 100) b
WHERE (LENGTH(Col2)-LENGTH(REPLACE(Col2,',','')))+1 >=lev
ORDER BY Col1,lev

SQL Server 분할 병합 표 방법
클릭 하여 링크 열기
원본 링크:http://blog.csdn.net/roy_88/article/details/6930577

좋은 웹페이지 즐겨찾기