1

I have a table having single column named combined_data as shown below :-

Combined_data

101

SAM

INDIA

102

ROCK

USA

103

MICHAEL

RUSSIA

AND I NEED THE OUTPUT LIKE:-

ID FNAME COUNTRY

101 SAM INDIA

102 ROCK USA

103 MICHAEL RUSSIA

How could I achieve this? Tried using pivot but it fetches min and max records so I get only 2 rows I need all the rows...

1
  • Simply, you can't do that, not if data you posted looks exactly like that. Rows in a RDBMS table are unordered so - if there's nothing you can order them by, there's no guarantee that you'll fetch them as you posted. Bad luck, eh? Commented Jul 14, 2020 at 15:18

1 Answer 1

2

Just number your rows, precompute index of each new row and column and put it together.

with combined_data (id,val) as (
  select 1, '101' from dual union all
  select 2, 'SAM' from dual union all
  select 3, 'INDIA' from dual union all
  select 4, '102' from dual union all
  select 5, 'ROCK' from dual union all
  select 6, 'USA' from dual union all
  select 7, '103' from dual union all
  select 8, 'MICHAEL' from dual union all
  select 9, 'RUSSIA' from dual
), n as (
  select id, val, row_number() over (order by id) - 1 as rn 
  from combined_data
), rc as (
  select id, val, mod(rn, 3) as c, (rn - mod(rn, 3))/ 3 as r 
  from n
)
select t1.val, t2.val, t3.val
from rc t1 join rc t2 using (r) join rc t3 using (r)
where t1.c = 0 and t2.c = 1 and t3.c = 2

UPDATE: the solution based on pivot clause:

with combined_data (id,val) as (
  select 1, '101' from dual union all
  select 2, 'SAM' from dual union all
  select 3, 'INDIA' from dual union all
  select 4, '102' from dual union all
  select 5, 'ROCK' from dual union all
  select 6, 'USA' from dual union all
  select 7, '103' from dual union all
  select 8, 'MICHAEL' from dual union all
  select 9, 'RUSSIA' from dual
), n as (
  select id, val, row_number() over (order by id) - 1 as rn 
  from combined_data
), rc as (
  select val, mod(rn, 3) as c, (rn - mod(rn, 3))/ 3 as r 
  from n
)
select num, name, state from rc
pivot (min(val) for c in (0 num, 1 name, 2 state))
order by r

(Note the rc CTE must not have id column here. Otherwise it would be part of implicit group by performed by pivot clause.)

See db fiddle.

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

3 Comments

Nice try, but - unfortunally - in vain. The OP said: "I have a table having single column". There's no id there.
@Littlefoot I read: (Assume ID column as primary key if needed) (hidden in poorly formatted data). Without ordering-imposing column the question does not make sense. With such column it is quite interesting question.
Ah, right ... below Russia. Didn't even see it (and someone upvoted the question? For what reason)? Anyway: in that case, +1.

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.