如何使用oracle sql将两列拆分为两行?

时间:2016-01-15 00:12:16

标签: sql oracle

例如,我有以下数据:

column1    column2      column3    column4     column5
A            B             5          2           3

我想要的表格是:

columnA
A             
B

确定分裂的条件是if(column3 + column4)> 5否则不分裂。

4 个答案:

答案 0 :(得分:3)

union all

select column1 as columnA from t where column3 + column4 > 5 
union all
select column2 from t where column3 + column4 > 5 ;

答案 1 :(得分:0)

我不确定我是否完全理解你在寻找什么,但如果你想要的话。在SELECT中放置一个if,ele,你可能不想使用CASE表达式。

https://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm

答案 2 :(得分:0)

对包含2行的派生表使用LEFT JOIN(带连接条件)或CROSS JOIN(无连接条件),然后通过case表达式输出值。这两种方法都允许访问其他条件的所有列。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE TABLE1
    (COLUMN1 VARCHAR2(1), COLUMN2 VARCHAR2(1), COLUMN3 INT, COLUMN4 INT, COLUMN5 INT)
;

INSERT ALL 
    INTO TABLE1 (COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5)
         VALUES ('A', 'B', 5, 2, 3)
    INTO TABLE1 (COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5)
         VALUES ('C', 'D', 1, 1, 1)
SELECT * FROM DUAL

查询1

select
    case when lj.rn = 1 then t.column1 else t.column2 end as columnA
    , t.column1
    , t.column2
    , t.column3
    , t.column4
    , t.column5
from table1 t
left join ( select 1 as rn from dual union all select 2 from dual ) lj 
      on (column3 + column4) > 5 

<强> Results

| COLUMNA | COLUMN1 | COLUMN2 | COLUMN3 | COLUMN4 | COLUMN5 |
|---------|---------|---------|---------|---------|---------|
|       A |       A |       B |       5 |       2 |       3 |
|       B |       A |       B |       5 |       2 |       3 |
|       D |       C |       D |       1 |       1 |       1 |

查询2

select
    case when cj.rn = 1 then t.column1 else t.column2 end as columnA
    , t.column1
    , t.column2
    , t.column3
    , t.column4
    , t.column5
from table1 t
cross join ( select 1 as rn from dual union all select 2 from dual ) cj

<强> Results

| COLUMNA | COLUMN1 | COLUMN2 | COLUMN3 | COLUMN4 | COLUMN5 |
|---------|---------|---------|---------|---------|---------|
|       A |       A |       B |       5 |       2 |       3 |
|       B |       A |       B |       5 |       2 |       3 |
|       C |       C |       D |       1 |       1 |       1 |
|       D |       C |       D |       1 |       1 |       1 |

答案 3 :(得分:0)

假设我有表test包含样本数据

column1  |  column2  |  column3  |  column4  |  column5  
-------------------------------------------------------  
A        |  B        |  5        |  2        |  3  
C        |  D        |  1        |  1        |  1  
E        |  F        |  4        |  5        |  1  

以上示例数据的脚本: -

CREATE TABLE TEST  
(  
 column1 VARCHAR2(10),  
 column2 VARCHAR2(10),  
 column3 NUMBER(2),  
 column4 NUMBER(2),  
 column5 NUMBER(2)  
);  

INSERT INTO TEST VALUES('A','B',5,2,3);  
INSERT INTO TEST VALUES('C','D',1,1,1);  
INSERT INTO TEST VALUES('E','F',4,5,1);  
COMMIT;  

以下查询将提供所需的输出: -

WITH tmp AS  
(SELECT (CASE   
          WHEN column3 + column4 > 5 THEN  
            column1||','||column2  
          ELSE  
            NULL  
        END) columna,  
        column1,column2,column3,column4,column5  
  FROM TEST)  
SELECT regexp_substr(columna,'[^,]+',1,LEVEL) columna,  
       column1,column2,column3,column4,column5  
  FROM tmp  
CONNECT BY LEVEL <= regexp_count(columna, ',') + 1  
    AND PRIOR column1 = column1  
    AND PRIOR dbms_random.random IS NOT NULL;

<强>输出:

columnA  |  column1  |  column2  |  column3  |  column4  |  column5  
-------------------------------------------------------------------  
A        |  A        |  B        |  5        |  2        |  3    
B        |  A        |  B        |  5        |  2        |  3  
         |  C        |  D        |  1        |  1        |  1  
E        |  E        |  F        |  4        |  5        |  1    
F        |  E        |  F        |  4        |  5        |  1