row-column conversion

Listagg function (row and column conversion) of new features of Oracle11.2

As aggregate function
SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP(ORDER BY ename) AS employees
FROM scott.emp
GROUP BY deptno;

 

Multiple tables
select jzg.tid,
       jzg.firstname,
       listagg(d.dwmc, ',') within group(order by d.dwmc) aa
  from xtgl_jzg jzg, xtgl_yhdwgxb gx, xtgl_depart d
 where jzg.tid = gx.yhbh(+)
   and gx.dwh = d.dwdm(+)
 GROUP BY jzg.tid, jzg.firstname

 

Multiple tables, and the existence of blob type fields in the table, self-association

select x.*, j.aa
  from (select jzg.tid,
               jzg.firstname,
               listagg(d.dwmc, ',') within group(order by d.dwmc) aa
          from xtgl_jzg jzg, xtgl_yhdwgxb gx, xtgl_depart d
         where jzg.tid = gx.yhbh(+)
           and gx.dwh = d.dwdm(+)
         GROUP BY jzg.tid, jzg.firstname) j,
       xtgl_jzg x
 where j.tid = x.tid
 order by x.tid

 --Excerpt from User Management of Mobile IM Interface

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326593724&siteId=291194637