Dear experts,
Input
-------
Create TABLE #tmp(Section CHAR(4),Sno INT,Name VARCHAR(20),Value1 VARCHAR(20),Value2 VARCHAR(20),id INT)
INSERT #tmp VALUES ('sec1',1,'A','1','5',1000)
INSERT #tmp VALUES ('sec1',1,'A','1','10',1000)
INSERT #tmp VALUES ('sec1',1,'B','2','5',1000)
INSERT #tmp VALUES ('sec1',1,'B','2','10',1000)
INSERT #tmp VALUES ('sec1',1,'B','2','15',1000)
INSERT #tmp VALUES ('sec1',2,'C','1','5',1000)
INSERT #tmp VALUES ('sec1',2,'C','1','10',1000)
INSERT #tmp VALUES ('sec1',2,'D','2','5',1000)
INSERT #tmp VALUES ('sec1',2,'D','2','10',1000)
INSERT #tmp VALUES ('sec1',2,'E','3','5',1000)
INSERT #tmp VALUES ('sec1',2,'E','3','10',1000)
INSERT #tmp VALUES ('sec1',2,'E','3','15',1000)
INSERT #tmp VALUES ('sec2',1,'F','1','5',1000)
INSERT #tmp VALUES ('sec2',1,'F','1','10',1000)
INSERT #tmp VALUES ('sec2',1,'F','1','15',1000)
INSERT #tmp VALUES ('sec2',1,'G','2','5',1000)
INSERT #tmp VALUES ('sec2',1,'G','2','10',1000)
INSERT #tmp VALUES ('sec2',2,'H','1','5',1000)
INSERT #tmp VALUES ('sec2',2,'H','1','10',1000)
-----------
INSERT #tmp VALUES ('sec1',1,'A','1','5',1001)
INSERT #tmp VALUES ('sec1',1,'A','1','10',1001)
INSERT #tmp VALUES ('sec1',1,'B','2','5',1001)
INSERT #tmp VALUES ('sec1',1,'B','2','10',1001)
INSERT #tmp VALUES ('sec1',1,'B','2','15',1001)
INSERT #tmp VALUES ('sec1',2,'C','1','5',1001)
INSERT #tmp VALUES ('sec1',2,'C','1','10',1001)
INSERT #tmp VALUES ('sec1',2,'D','2','5',1001)
INSERT #tmp VALUES ('sec1',2,'D','2','10',1001)
INSERT #tmp VALUES ('sec1',2,'E','3','5',1001)
INSERT #tmp VALUES ('sec1',2,'E','3','10',1001)
INSERT #tmp VALUES ('sec1',2,'E','3','15',1001)
INSERT #tmp VALUES ('sec2',1,'F','1','5',1001)
INSERT #tmp VALUES ('sec2',1,'F','1','10',1001)
INSERT #tmp VALUES ('sec2',1,'F','1','15',1001)
INSERT #tmp VALUES ('sec2',1,'G','2','5',1001)
INSERT #tmp VALUES ('sec2',1,'G','2','10',1001)
INSERT #tmp VALUES ('sec2',2,'H','1','5',1001)
INSERT #tmp VALUES ('sec2',2,'H','1','10',1001)
expected results
----------------------
Section | id | sno | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8 | Col9 |
sec1 | 1000 | 1 | A | 1 | 5 | B | 2 | 10 | NULL | NULL | NULL |
sec1 | 1000 | 1 | A | 1 | 10 | B | 2 | 15 | NULL | NULL | NULL |
sec1 | 1000 | 2 | C | 1 | 5 | D | 2 | 5 | E | 3 | 10 |
sec1 | 1000 | 2 | C | 1 | 10 | D | 2 | 10 | E | 3 | 15 |
sec2 | 1000 | 1 | F | 1 | 10 | G | 2 | 5 | NULL | NULL | NULL |
sec2 | 1000 | 1 | F | 1 | 15 | G | 2 | 10 | NULL | NULL | NULL |
sec2 | 1000 | 2 | H | 1 | 5 | NULL | NULL | NULL | NULL | NULL | NULL |
sec2 | 1000 | 2 | H | 1 | 10 | NULL | NULL | NULL | NULL | NULL | NULL |
sec1 | 1001 | 1 | A | 1 | 5 | B | 2 | 10 | NULL | NULL | NULL |
sec1 | 1001 | 1 | A | 1 | 10 | B | 2 | 15 | NULL | NULL | NULL |
sec1 | 1001 | 2 | C | 1 | 5 | D | 2 | 5 | E | 3 | 10 |
sec1 | 1001 | 2 | C | 1 | 10 | D | 2 | 10 | E | 3 | 15 |
sec2 | 1001 | 1 | F | 1 | 10 | G | 2 | 5 | NULL | NULL | NULL |
sec2 | 1001 | 1 | F | 1 | 15 | G | 2 | 10 | NULL | NULL | NULL |
sec2 | 1001 | 2 | H | 1 | 5 | NULL | NULL | NULL | NULL | NULL | NULL |
sec2 | 1001 | 2 | H | 1 | 10 | NULL | NULL | NULL | NULL | NULL | NULL |
Condition :
for values2 column if more than two values are present then display last two values .