1

How can I write a query for below scenario in Oracle?

Input

Column1 Column2
R        1
S        2
F        3

Output

RSSFFF

i.e. 'R' 1 time, 'S' 2 time and 'F' 3 times.

2
  • What query have you tried so far? Commented Oct 17, 2018 at 2:31
  • My actual requirement is very complex, and this is a part of that. i am not getting any idea to implement this. if you can suggest any approach or can provide any Hints, i can write the query. Commented Oct 17, 2018 at 2:34

2 Answers 2

3

You can use rpad to repeat the letters, and listagg to concatenate them in a single line, like so.

select
  listagg(rpad(column1,column2,column1)) within group (order by column2)
from table_name;

But this would work if you have another column to order the rows, in this case, I just use the column2.

Sign up to request clarification or add additional context in comments.

1 Comment

@Ganesh I edited my answer to remove the extra order by clause, which would work without it.
2

One method uses lpad():

select lpad(column1, column2, column1)
from t;

If you want a single string, then use listagg():

select listagg(lpad(column1, column2, column1)) within group (order by null)
from t;

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.