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

sql 面试问题?
表15
 a1 a2 
 1 a 
 1 b 
 2 x 
 2 y 
 2 z 
用select能显示成以下结果吗? 
1 ab 
2 xyz

------解决方案--------------------
ORACLE10G

SQL code
CREATE TABLE a
(a1 INT,
 a2 VARCHAR2(1));


INSERT INTO a VALUES(1,'a');
INSERT INTO a VALUES(1,'b');
INSERT INTO a VALUES(2,'x');
INSERT INTO a VALUES(2,'y');
INSERT INTO a VALUES(2,'z');

select a1, replace(wmsys.wm_concat(a2),',','') a2 from a group by a1

------解决方案--------------------
SQL code

with tb(a1,a2) as (
select 1,'a' from dual
union
select 1,'b' from dual
union
select 2,'x' from dual
union
select 2,'y' from dual
union
select 2,'z' from dual
)
select a1,replace(wm_concat(a2),',') as ass from tb group by a1
/

        A1 ASS
---------- ----------
         1 ab
         2 xzy

SQL>

------解决方案--------------------
列转行问题