日期:2014-05-17  浏览次数:20862 次

求SQL写法,每次查询同等的多条数据
假设有个表TBLA,
表里字段为 AA,BB,CC

我想取得的数据Result是这样的,AA,BB,CC,AA1,BB1,CC1,AA2,BB2,CC2
意思就是想要一次性取得三条数据,然后把后面2条数据的字段重命名一下,比如AA->AA1


------解决方案--------------------
写的有点麻烦..
SQL code

create table t1 (AA varchar2(20), BB varchar2(10), CC number(3));

insert into t1 values ('jack','A',80);
insert into t1 values ('rose','B',60);
insert into t1 values ('lucy','C',70);
insert into t1 values ('lili','A',95);
insert into t1 values ('lei','B',85);
insert into t1 values ('doufu','C',75);
commit;

select wm_concat(decode(rn,1,aa,null)) AA,wm_concat(decode(rn,1,bb,null)) bb,wm_concat(decode(rn,1,cc,null)) cc,
       wm_concat(decode(rn,2,aa,null)) AA1,wm_concat(decode(rn,2,bb,null)) bb1,wm_concat(decode(rn,2,cc,null)) cc1,
       wm_concat(decode(rn,3,aa,null)) AA2,wm_concat(decode(rn,3,bb,null)) bb2,wm_concat(decode(rn,3,cc,null)) cc2
from
(select AA,BB,CC,rownum rn from t1 where rownum<=3)

    AA    BB    CC    AA1    BB1   CC1   AA2   BB2   CC2
-----------------------------
1    jack    A    80    rose    B    60    lucy    C    70