日期:2014-05-16  浏览次数:20416 次

把纵列的相同数据分组成不同的横列
 select 
        CARRIERACCOUNDCODE, 
        max(decode(sectionId, 1,sectionId)) id1, 
        max(decode(sectionId, 1, setionName)) name1,

        max(decode(sectionId, 2,sectionId)) id2, 
        max(decode(sectionId, 2, setionName)) name2 ,

        max(decode(sectionId, 3,sectionId)) id3, 
        max(decode(sectionId, 3, setionName)) name3,

        max(decode(sectionId, 4,sectionId)) id4,
        max(decode(sectionId, 4, setionName)) name4,
        
         max(decode(sectionId, 5,sectionId)) id5,
        max(decode(sectionId, 5, setionName)) name5,
        
         max(decode(sectionId, 6,sectionId)) id6,
        max(decode(sectionId, 6, setionName)) name6
from (
        select 
            CARRIERACCOUNDCODE, 
            setionName,
            sectionId,
            row_number() over(partition by CARRIERACCOUNDCODE order by sectionId) rn, 
            count(*) over(partition by CARRIERACCOUNDCODE) cnt 

        from (SELECT t2.CARRIERACCOUNDCODE,
                        t2.sectionId,
                        t2.setionName
                FROM    carrier t1
                     LEFT JOIN
                        funcinfo t2
                     ON t1.CARRIERACCOUNTCODE = t2.CARRIERACCOUNDCODE 
                     where CARRIERACCOUNDCODE is not null
                 )
        )
  
     group by CARRIERACCOUNDCODE;
     

?