How can I align the rows and attributes of each field in a table to appear in the same line, using the given table as an example?. Mark this all fields in this table are varchar so as to enable uniformity.
STD | SUBJECT | MARKS | COMMENT | TDATE |
---|---|---|---|---|
ST1 | MATH | 25% | POOR | 1/02/2021 |
ST1 | ENGLISH | 88% | DIST | 2/02/2021 |
ST1 | SCIENCE | 56% | PASS | 4/02/2021 |
The table should appear like this after unpivoting the contents in [Subject]
:
STD | MATH | COMMENT | TDATE | ENGLISH | COMMENT | TDATE | SCIENCE | COMMENT | TDATE |
---|---|---|---|---|---|---|---|---|---|
ST1 | 25% | POOR | 1/02/2021 | 88% | DIST | 2/02/2021 | 56% | PASS | 4/02/2021 |
I tried with my code and got lots of errors, so maybe I could get some help to achieve the desired result.
SELECT
[MATH],
[ENGLISH],
[SCIENCE]
FROM (
SELECT
STD, stdn,
cont,
x,
SUBJECT
FROM
[dbo].[Exam]
UNPIVOT (
x
for cont in (COMMENT, TDATE)
) a
) a
PIVOT (
MAX(x)
FOR SUBJECT IN (
[MATH],
[ENGLISH],
[SCIENCE],
)
) p
WHERE p.stdn IN (SELECT STD FROM [dbo].[exam])