Hazel Mahmud

Hazel Mahmud

  • 1.4k
  • 315
  • 70.6k

Pivot with 3 tables without pivot function

Jul 14 2024 2:33 AM

How do i make pivot without using pivot function. Those data is related by parameter @semester. It can be any semester.

DECLARE @Semester VARCHAR(10)
    SET @Semester = '1'

CREATE TABLE #Students (
    MatricNo INT PRIMARY KEY,
    Name VARCHAR(100),
    Final1 INT,
    Grade varchar(2)
)

INSERT INTO #Students VALUES(888999, 'Hazel', 221, 'F')

CREATE TABLE #Marks(
    MatricNo INT,
    Semester VARCHAR(10),
    Test VARCHAR(50),
    Marks INT
)

INSERT INTO #Marks VALUES(888999, 1, '1', 15)
INSERT INTO #Marks VALUES(888999, 1, '2', 10)
INSERT INTO #Marks VALUES(888999, 1, '3', 10)
INSERT INTO #Marks VALUES(888999, 1, '4', 20)

INSERT INTO #Marks VALUES(888999, 2, '1', 25)
INSERT INTO #Marks VALUES(888999, 2, '2', 20)
INSERT INTO #Marks VALUES(888999, 2, '3', 30)

CREATE TABLE #Test(
    TestID INT,
    Test Name VARCHAR(40)
)

INSERT INTO #TestVALUES(1, 'Assignment_1')
INSERT INTO #TestVALUES(2, 'Assignment_2')
INSERT INTO #TestVALUES(3, 'Presentation')
INSERT INTO #TestVALUES(4, 'Project')

Finally, it can be either 1st look

Matric No. | Name | Assignment 1 | Assignment 2 | Presentation | Project | Final |  Grade |  Point

  888999   | Hazel |        15           |        10           |         10         |    20     |    45   |      F     |     0

OR  2nd Look

Matric No. | Name | Assignment 1 | Assignment 2 |  Final | Grade |  Point

  888999   | Hazel |          15          |         10           |    45   |     F     |     0

 


Answers (6)