SQL code: IF OBJECT_ID('TB') IS NOT NULL DROP TABLE TB IF OBJECT_ID('TEMPDB..#') IS NOT NULL DROP TABLE # GO CREATE TABLE TB(VAL VARCHAR(50)) INSERT INTO TB SELECT 'A' UNION ALL SELECT 'B' UNION ALL SELECT 'C' UNION ALL SELECT 'D' UNION ALL SELECT 'E' UNION ALL SELECT 'F' UNION ALL SELECT 'G' UNION ALL SELECT 'H' UNION ALL SELECT 'I' UNION ALL SELECT 'J' UNION ALL SELECT 'K' UNION ALL SELECT 'L' UNION ALL SELECT 'M' UNION ALL SELECT 'N'
SELECT IDENTITY(INT,0,1) 'ID',VAL INTO # from TB
SELECT MAX(CASE WHEN ID%5=0 THEN VAL ELSE NULL END) ,MAX(CASE WHEN ID%5=1 THEN VAL ELSE NULL END) ,MAX(CASE WHEN ID%5=2 THEN VAL ELSE NULL END) ,MAX(CASE WHEN ID%5=3 THEN VAL ELSE NULL END) ,MAX(CASE WHEN ID%5=4 THEN VAL ELSE NULL END) from # GROUP BY ID/5
/* A B C D E F G H I J K L M N NULL */
SQL code: create table tb(id int, data int) insert into tb values(1 , 1) insert into tb values(2 , 2) insert into tb values(3