Hi makumbi,
Please refer below sample.
SQL
CREATE TABLE #Archivedreports
(
[Name] VARCHAR (50),
[Engscore] VARCHAR (50),
[Engrade] VARCHAR (50),
[Mathsscore] VARCHAR (50),
[Mathsgrade] VARCHAR (50),
[Sciencescore] VARCHAR (50),
[Sciencegrade] VARCHAR (50),
[Type] VARCHAR (50),
[Monthe] VARCHAR (50)
)
INSERT INTO #Archivedreports VALUES ('ABC','52','A','40','A+','52','B+','1','Jan')
INSERT INTO #Archivedreports VALUES ('ABC','45','B','47','B+','87','A+','2','Feb')
INSERT INTO #Archivedreports VALUES ('ABC','85','C','52','C+','45','C+','3','Mar')
INSERT INTO #Archivedreports VALUES ('ABC','25','D','75','A+','78','A+','4','Apr')
SELECT TOP (100) PERCENT Name, engscore 'Score', engrade 'Grade', 'ENG' AS Subject, Type, Monthe
FROM #Archivedreports
WHERE (Type IS NOT NULL)
UNION
SELECT TOP (100) PERCENT Name, mathsscore, mathsgrade, 'MATHS' AS Subject, Type, Monthe
FROM #Archivedreports
WHERE (Type IS NOT NULL)
UNION
SELECT TOP (100) PERCENT Name, sciencescore, sciencegrade, 'SCIE' AS Subject, Type, Monthe
FROM #Archivedreports
WHERE (Type IS NOT NULL)
UNION
SELECT TOP (100) PERCENT Name, sciencescore, sciencegrade, 'SST' AS Subject, Type, Monthe
FROM #Archivedreports
WHERE (Type IS NOT NULL)
ORDER BY Type
OutPut
Name |
Score |
Grade |
Subject |
Type |
Monthe |
ABC |
40 |
A+ |
MATHS |
1 |
Jan |
ABC |
52 |
A |
ENG |
1 |
Jan |
ABC |
52 |
B+ |
SCIE |
1 |
Jan |
ABC |
52 |
B+ |
SST |
1 |
Jan |
ABC |
45 |
B |
ENG |
2 |
Feb |
ABC |
47 |
B+ |
MATHS |
2 |
Feb |
ABC |
87 |
A+ |
SCIE |
2 |
Feb |
ABC |
87 |
A+ |
SST |
2 |
Feb |
ABC |
45 |
C+ |
SCIE |
3 |
Mar |
ABC |
45 |
C+ |
SST |
3 |
Mar |
ABC |
52 |
C+ |
MATHS |
3 |
Mar |
ABC |
85 |
C |
ENG |
3 |
Mar |
ABC |
25 |
D |
ENG |
4 |
Apr |
ABC |
75 |
A+ |
MATHS |
4 |
Apr |
ABC |
78 |
A+ |
SCIE |
4 |
Apr |
ABC |
78 |
A+ |
SST |
4 |
Apr |