0

for example I have a data below:

column1    column2      column3    column4     column5
A            B             5          2           3

My desired table would be:

columnA
A             
B

The condition to determine the splitting is if (column3 + column4)>5 otherwise no splitting.

2
  • Sample data and desired results would help convey what you want to do. Commented Jan 15, 2016 at 0:20
  • What do you mean by "otherwise no splitting"? They should appear individually on the output? In this case, with which columns? Commented Jan 15, 2016 at 1:04

4 Answers 4

3

union all:

select column1 as columnA from t where column3 + column4 > 5 
union all
select column2 from t where column3 + column4 > 5 ;
Sign up to request clarification or add additional context in comments.

1 Comment

but I have a condition based on column 3 and 4
0

I'm not sure I fully understood what you are looking for, but if you wan' to put a if, ele in the SELECT, you might wan't to use the CASE expression.

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

Comments

0

Use a LEFT JOIN (with join condition) or CROSS JOIN (no join condition) to a derived table that consists of 2 rows, then output the values via a case expression. Both approaches allow access to all columns for other conditions too.

SQL Fiddle

Oracle 11g R2 Schema Setup:

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

Query 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 |

Query 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 |

Comments

0

Suppose I have table test with sample data

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

Script for above sample data:-

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;  

Below query will give required output:-

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;

Output:

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  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.