Oracle 10gR2 行变列研究

  Oracle 10gR2 行变列研究

    建立测试数据和环境:

create table test(
 COUNTRY 
varchar2(20),
 CITY  
varchar2(20) )
insert into test(COUNTRY,CITY) VALUES('中国','上海');
commit;
insert into test(COUNTRY,CITY) VALUES('中国','湖北');
commit;
insert into test(COUNTRY,CITY) VALUES('中国','天津');
commit;
insert into test(COUNTRY,CITY) VALUES('美国','纽约');
commit;
insert into test(COUNTRY,CITY) VALUES('美国','华盛顿');
commit;

   实现结果:

中国       天津,湖北,上海
美国       华盛顿,纽约

 编写代码如下:

select country, 
max(decode(city,'上海',city,null))||','||max(decode(city,'湖北',city,null))
||','||max(decode(city,'天津',city,null))  from test  GROUP by country

 

 

posted @ 2008-10-14 21:23  zping  阅读(807)  评论(0编辑  收藏  举报