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

oracle列转行的问题(在线等)
客户 帐龄1 帐龄2 帐龄3
A客户 15 45 75
A客户 20 50 80

转换成 
客户 帐龄
A客户 15
A客户 45
A客户 75
A客户 20
A客户 50
A客户 80


请问需要怎么写?

------解决方案--------------------
SQL code
select 客户,帐龄1 from 表
union all
select 客户,帐龄2 from 表
union all
select 客户,帐龄3 from 表

------解决方案--------------------
http://zhidao.baidu.com/question/170746889.html
------解决方案--------------------
SQL code
selelect 客户,帐龄1 AS 帐龄,帐龄類型='帐龄1' FROM Tablename
selelect 客户,帐龄2    AS 帐龄,帐龄類型='帐龄2' FROM Tablename
selelect 客户,帐龄3 AS 帐龄,帐龄類型='帐龄3'FROM Tablename

------解决方案--------------------
改改,輸入法太快亂來了,看看是否為這樣
SQL code
SELECT  客户,帐龄1 AS 帐龄,帐龄類型='帐龄1' FROM Tablename UNION ALL 
SELECT 客户,帐龄2    AS 帐龄,帐龄類型='帐龄2' FROM Tablename UNION ALL 
SELECT 客户,帐龄3 AS 帐龄,帐龄類型='帐龄3'FROM Tablename

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

--方法 1:
with tbl as
(
    select 'A客户' as "客户", 15 as "账龄1", 45 as "账龄2", 75 as "账龄3" from dual
     union all
    select 'A客户' as "客户", 20 as "账龄1", 50 as "账龄2", 80 as "账龄3" from dual
)
select "客户", "账龄1" as "账龄" from tbl
 union all
select "客户", "账龄2" from tbl
 union all
select "客户", "账龄3" from tbl;

客户            账龄
------- ----------
A客户           15
A客户           20
A客户           45
A客户           50
A客户           75
A客户           80

--方法 2:
with tbl as
(
    select 'A客户' as "客户", 15 as "账龄1", 45 as "账龄2", 75 as "账龄3" from dual
     union all
    select 'A客户' as "客户", 20 as "账龄1", 50 as "账龄2", 80 as "账龄3" from dual
)
select "客户", regexp_substr("账龄", '[^,]+', 1, line) as "账龄"
  from (select "客户", "账龄1" || ',' || "账龄2" || ',' || "账龄3" as "账龄" from tbl) t1,
       (select rownum as line from dual connect by rownum <= 3) t2
 where regexp_substr("账龄", '[^,]+', 1, line) is not null;

客户    账龄   
------- -------
A客户   15     
A客户   20     
A客户   45     
A客户   50     
A客户   75     
A客户   80

------解决方案--------------------
用Case吧
select case when ... then ... end case from Tablename