CREATE TABLE #thetable
( id INT IDENTITY(1,1) NOT NULL ,
headings1 VARCHAR(100),
headings2 VARCHAR(100),
)
INSERT INTO #thetable (headings1, headings2) VALUES ('Amikacin','Sensitivity')
INSERT INTO #thetable (headings1, headings2) VALUES ('Ampicillin','Resistant')
INSERT INTO #thetable (headings1, headings2) VALUES ('Cefadroxil','Intermediate')
INSERT INTO #thetable (headings1, headings2) VALUES ('Cefoperazone','Resistant')
INSERT INTO #thetable (headings1, headings2) VALUES ('Chloramphenicol','Resistant')
INSERT INTO #thetable (headings1, headings2) VALUES ('Netromycin','Sensitivity')
SELECT * FROM #thetable
-- this will work if you don't care about the NULL values and the position of the itmes
SELECT
CASE WHEN headings2 = 'Sensitivity' THEN headings1 END AS 'Sensitivity'
,CASE WHEN headings2 = 'Resistant' THEN headings1 END AS 'Resistant'
,CASE WHEN headings2 = 'Intermediate' THEN headings1 END AS 'Intermediate'
FROM #thetable
-- this will have all NULL values at the bottom
SELECT
t1.[Sensitivity]
,t2.[Resistant]
,t3.[Intermediate]
FROM
(
SELECT
ROW_NUMBER() OVER (ORDER BY headings1) AS rn
,headings1 AS 'Sensitivity'
FROM #thetable
WHERE headings2 = 'Sensitivity'
) t1 FULL JOIN
(
SELECT
ROW_NUMBER() OVER (ORDER BY headings1) AS rn
,headings1 AS 'Resistant'
FROM #thetable
WHERE headings2 = 'Resistant'
) t2 ON t1.rn = t2.rn FULL JOIN
(
SELECT
ROW_NUMBER() OVER (ORDER BY headings1) AS rn
,headings1 AS 'Intermediate'
FROM #thetable
WHERE headings2 = 'Intermediate'
) t3 ON t1.rn = t3.rn