2

I have a row that should be duplicated in certain conditions. Example:

Case 1:

a  b  c   d   e
---------------
1  4  25  10  NULL

if e is Null, display a, b and c:

1 4 25

Case 2:

a  b  c   d   e
---------------
1  4  25  10  55

if e is not Null, duplicate the row

1 4 25 => column a,b,c

1 4 10 => column a,b,d

2 Answers 2

3

Use this query

Using union you can achieve this use case

select a,b,c from table
union all
select a,b,d from table where e is not null
Sign up to request clarification or add additional context in comments.

1 Comment

If this answer solves your problem mark it as accepted answer. So others may benefit @zied123456
1

If you don't want to scan the table twice, then you can use cross joinand some logic:

select a, b, 
       (case when n = 1 then c else d end)
from t cross join
     (select 1 as n union all select 2) n
where n = 1 or
      (n = 2 and e is not null);

1 Comment

the where condition should be where n = 1 or (n = 2 and e is not null)

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.