Show remarks in sql based on comparing five rounds of examination

Last Reply 6 days ago By AjayV

Posted 13 days ago

I have a following data.

tblTestSystem

AdmissionNo

ClassID

SubjectID

RoundID

Marks

TotalMarks

TestDate

1

101

201

1

5

10

01-03-23

2

101

201

1

7

10

01-03-23

3

101

201

1

8

10

01-03-23

1

101

202

1

6

10

02-03-23

2

101

202

1

2

10

02-03-23

3

101

202

1

5

10

02-03-23

1

101

203

1

9

10

03-03-23

2

101

203

1

8

10

03-03-23

3

101

203

1

6

10

03-03-23

1

101

201

2

5

10

04-03-23

2

101

201

2

7

10

04-03-23

3

101

201

2

8

10

04-03-23

1

101

202

2

6

10

05-03-23

2

101

202

2

2

10

05-03-23

3

101

202

2

5

10

05-03-23

1

101

203

2

9

10

06-03-23

2

101

203

2

8

10

06-03-23

3

101

203

2

6

10

06-03-23

1

101

201

3

5

10

07-03-23

2

101

201

3

7

10

07-03-23

3

101

201

3

8

10

07-03-23

1

101

202

3

6

10

08-03-23

2

101

202

3

2

10

08-03-23

3

101

202

3

5

10

08-03-23

1

101

203

3

9

10

09-03-23

2

101

203

3

8

10

09-03-23

3

101

203

3

6

10

09-03-23

Required output 1

AdmissionNo

ClassID

RoundID

Subject 1

Subject 2

Subject 3

ObtMarks

TotalMarks

1

101

1

5

6

9

20

30

1

101

2

5

6

9

20

30

1

101

3

5

6

9

20

30

2

101

1

7

2

8

17

30

2

101

2

7

2

8

17

30

2

101

3

7

2

8

17

30

3

101

1

8

5

6

19

30

3

101

2

8

5

6

19

30

3

101

3

8

5

6

19

30

Required output 2:

I want to declare students perfomance in statemenets based on his five round results like that.

He is improving positively based on his five round results. Moreover he need improvments in (Here goes subject name which % is below 33 for example Subject1 , Subject 3) 

His perfomance is decreasing based on his five round results.Moreover, He need great effort to improve.

Note: All staments should be declared based on percentage performace while comparing all rounds for individual students.

AdmissionNo        Remarks

R-1             He is improving positively based on his five round                              results. Moreover he need improvments in (Here goes                        subject name which % is below 33 for example Subject1                   , Subject 3) 

R-2            His perfomance is decreasing based on his five round                         results.Moreover, He need great effort to improve.

You are viewing reply posted by: AjayV 6 days ago.
Posted 6 days ago Modified on 6 days ago

Hi smile,

Please refer below sql query.

SQL
CREATE TABLE tblTestSystem
(
    AdmissionNo INT,
    ClassID     INT,
    SubjectID   INT,
    RoundID     INT,
    Marks       INT,
    TotalMarks  INT,
    TestDate    VARCHAR(50)
)
TRUNCATE TABLE tblTestSystem
INSERT INTO tblTestSystem VALUES(1,101,201,1,5,10,'1/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,201,1,7,10,'1/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,201,1,8,10,'1/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,202,1,7,10,'2/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,202,1,2,10,'2/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,202,1,5,10,'2/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,203,1,9,10,'3/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,203,1,6,10,'3/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,203,1,6,10,'3/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,201,2,5,10,'4/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,201,2,7,10,'4/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,201,2,9,10,'4/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,202,2,6,10,'5/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,202,2,6,10,'5/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,202,2,5,10,'5/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,203,2,9,10,'6/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,203,2,8,10,'6/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,203,2,7,10,'6/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,201,3,5,10,'7/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,201,3,4,10,'7/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,201,3,8,10,'7/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,202,3,7,10,'8/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,202,3,2,10,'8/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,202,3,5,10,'8/3/2023')
INSERT INTO tblTestSystem VALUES(1,101,203,3,9,10,'9/3/2023')
INSERT INTO tblTestSystem VALUES(2,101,203,3,3,10,'9/3/2023')
INSERT INTO tblTestSystem VALUES(3,101,203,3,7,10,'9/3/2023')
 
SELECT t1.admissionno,t1.classid,CONCAT(t2.SubjectName,'(',t1.TotalMarks,')') AS subjectid,t1.roundid,t1.marks,t1.totalmarks,t1.testdate  into tblTestSystem2 FROM  tblTestSystem t1 join tblDefSubjects t2 ON t2.SubjectId = t1.SubjectID

DECLARE @DynamicPivotQuery2 AS NVARCHAR(MAX)
DECLARE @ColumnName2 AS NVARCHAR(MAX)
SELECT @ColumnName2 = ISNULL(@ColumnName2 + ',','')+ QUOTENAME(SubjectID) FROM (SELECT DISTINCT SubjectID FROM tblTestSystem2 ) AS FeesHead
 
SET @DynamicPivotQuery2 = '
;WITH CTE AS(
    SELECT RoundID,fa.AdmissionNo,Marks,SubjectID,ClassID
    ,(SELECT SUM(tsm.Marks) FROM tblTestSystem2 tsm where tsm.AdmissionNo = fa.AdmissionNo AND tsm.RoundID = fa.RoundID ) as Obtain
    ,(SELECT SUM(tsm.TotalMarks) FROM tblTestSystem2 tsm where tsm.AdmissionNo = fa.AdmissionNo AND tsm.RoundID = fa.RoundID) as Total
    FROM tblTestSystem2 as fa
)
SELECT AdmissionNo,ClassID,RoundID,'+@ColumnName2+',Obtain,Total INTO temp1 FROM CTE
PIVOT (MAX(Marks)
FOR SubjectID IN('+@ColumnName2+')) p order by AdmissionNo asc'
EXEC(@DynamicPivotQuery2);

SELECT DISTINCT AdmissionNo,
	CASE
		WHEN ((SELECT SUM(Obtain) FROM temp1 WHERE RoundID < 3 and AdmissionNo=1) <= (SELECT SUM(Obtain) FROM temp1 WHERE RoundID > 1 and AdmissionNo=1)) and AdmissionNo = 1  THEN 'He is improving positively'
		WHEN ((SELECT SUM(Obtain) FROM temp1 WHERE RoundID < 3 and AdmissionNo=2) <= (SELECT SUM(Obtain) FROM temp1 WHERE RoundID > 1 and AdmissionNo=2)) and AdmissionNo = 2  THEN 'He is improving positively'
		WHEN ((SELECT SUM(Obtain) FROM temp1 WHERE RoundID < 3 and AdmissionNo=3) <= (SELECT SUM(Obtain) FROM temp1 WHERE RoundID > 1 and AdmissionNo=3)) and AdmissionNo = 3  THEN 'He is improving positively'
		ELSE 'His perfomance is decreasing '
	END AS Remark
FROM temp1

Output

temp Table

AdmissionNo ClassID RoundID English(10) Math(10) Urdu(10) Obtain Total
1 101 1 5 9 7 21 30
1 101 2 5 9 6 20 30
1 101 3 5 9 7 21 30
2 101 1 7 6 2 15 30
2 101 2 7 8 6 21 30
2 101 3 4 3 2 9 30
3 101 1 8 6 5 19 30
3 101 2 9 7 5 21 30
3 101 3 8 7 5 20 30

Output2

AdmissionNo Remark
1 He is improving positively
2 His perfomance is decreasing 
3 He is improving positively