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

 

 

相关文章:

  • 2022-01-24
  • 2022-12-23
  • 2021-10-19
  • 2021-11-10
  • 2021-04-22
  • 2021-11-14
  • 2021-05-12
猜你喜欢
  • 2021-06-23
  • 2021-12-11
  • 2022-02-12
  • 2021-09-24
  • 2021-08-25
  • 2021-12-20
  • 2021-08-08
相关资源
相似解决方案