2

I have a table with only one column about 100 rows of only names. But I need to display the 3 names in a row. So that I will get 34 rows each row with 3 names.

Example:

Name
_____

Raj
Sam
Guru
Tej
Avin
Sami
Fanst

I need to display above data as

Name Name1 Name2
____ _____ ______
Raj  Sam    Guru
Tej  Avin   Sami
Fanst

No condition just need to covert single column value into 3 columns data.

Oracle DB

2
  • What database are you using? Each database has a slightly different SQL syntax so each will handle pivoting a little differently Commented Mar 17, 2015 at 3:32
  • Have you attempted to solve this yourself? Commented Mar 17, 2015 at 3:39

2 Answers 2

2

You can do this using conditional aggregation and rownum. Something like this:

select max(case when mod(rn, 3) = 1 then name end) as name1,
       max(case when mod(rn, 3) = 2 then name end) as name2,
       max(case when mod(rn, 3) = 0 then name end) as name3       
from (select name, rownum as rn
      from table t
     ) t
group by trunc((rn - 1) / 3);
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried way Gordon said but cannot split into 3 colums as I specified. Let me try wat Gordon said.
0

You can do it using CASE, make a try on it using PIVOT

Try to PIVOT Your Name column like this

SELECT Name,Name1,Name2
FROM
(SELECT Name FROM table_name) AS SourceTable
PIVOT
(
FOR Name IN (Name,Name1,Name2)
) AS PivotTable;

1 Comment

I don't think you can use PIVOT in this situation as you would not know the values to specify in advance.

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.