5

My table has data with a single column:

1
2
3
4
5
6
7
8
9
10

And I want output like this:

1 2 3 4 5-first row
6 7 8 9 10 - second row

Another has two columns like this:

Column 1 Column 2
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j

And I want it to display like this:

1 a 2 b 3 c 4 d 5 e-first row
6 f 7 g 8 h 9 i 10 j - second row
Aaron Bertrand
182k28 gold badges406 silver badges625 bronze badges
asked Jun 15, 2013 at 17:41

1 Answer 1

7

For the first case you can use ROW_NUMBER then PIVOT to do this using the modulo operator to divide into columns and the result of integer divsion by 5 to group into rows.

WITH CTE AS
(
SELECT C,
 (ROW_NUMBER() OVER (ORDER BY C) -1)%5 AS Col,
 (ROW_NUMBER() OVER (ORDER BY C) -1)/5 AS Row
FROM YourTable 
)
SELECT [0], [1], [2], [3], [4]
FROM CTE
PIVOT (MAX(C) FOR Col IN ([0], [1], [2], [3], [4])) AS Pvt
ORDER BY Row

SQL Fiddle

For the second case Oracle has a straight forward syntax to achieve this with PIVOT. SQL Server doesn't but you can do this as below.

WITH CTE AS
(
SELECT *,
 (ROW_NUMBER() OVER (ORDER BY Column1) -1)%5 AS Col,
 (ROW_NUMBER() OVER (ORDER BY Column1) -1)/5 AS Row
FROM YourTable 
)
SELECT MAX(CASE WHEN Col = 0 THEN Column1 END),
 MAX(CASE WHEN Col = 0 THEN Column2 END),
 MAX(CASE WHEN Col = 1 THEN Column1 END),
 MAX(CASE WHEN Col = 1 THEN Column2 END),
 MAX(CASE WHEN Col = 2 THEN Column1 END),
 MAX(CASE WHEN Col = 2 THEN Column2 END),
 MAX(CASE WHEN Col = 3 THEN Column1 END),
 MAX(CASE WHEN Col = 3 THEN Column2 END),
 MAX(CASE WHEN Col = 4 THEN Column1 END),
 MAX(CASE WHEN Col = 4 THEN Column2 END)
FROM CTE
GROUP BY Row
ORDER BY Row

SQL Fiddle

answered Jun 15, 2013 at 18:36
8
  • If its have two column means how to perform this opertaion? Commented Jun 16, 2013 at 7:33
  • @SureshSurya - Please edit your question to include example data and desired results for this case. Commented Jun 16, 2013 at 9:11
  • @MartinSmith Great answer, I would replace the pseudo SELECT * with SELECT Column1, Column2 for the sake of good habit... :) Commented Jun 16, 2013 at 13:02
  • @Justicator - I used to do that but now I'm more relaxed about * in places where it does not make any difference. In the first query in my answer it would make a difference as any new columns added to YourTable would silently be added to the implicitly defined GROUP BY of the PIVOT likely causing the query to return incorrect results. In the second case even if a new column was added SQL Server wouldn't unnecessarily retrieve it. It recognises at compile time that only Column1 and Column2 would be used. Commented Jun 16, 2013 at 14:06
  • @MartinSmith You are correct of course, But... I work with programmers and find it easier to "force" them to always use column names than to make them think before and decide each time. I also think that not everyone who reads an answer here understands when it makes no difference and why (well, now they know...), therefor it was just "for the sake of good habit". This is definitely not to lessen from your really great answer. Commented Jun 16, 2013 at 14:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.