How to accomplish this task using SQL Server
- If a student has two
studentsubjectstatus
, aMerit
studentperformance
-Merit
. - If a student has two
studentsubjectstatus
, andPass
studentperformance
-Pass
- Else is
Fail
So, I need a new studentperformance command and the result will be like this:
studentname studentperformance
A Fail
B Merit
C Pass
D Fail
source to share
You can write your query like:
;WITH CTE AS
( SELECT studentname,
ROW_NUMBER() OVER (partition BY studentname,studentsubjectstatus ORDER BY educationmarks DESC)
AS rownum,
studentsubjectstatus
FROM @student)
,CTE2 AS
(
SELECT studentname , MAX(rownum) AS maxrow
FROM CTE
GROUP BY studentname
)
SELECT DISTINCT T1.studentname
,CASE WHEN T1.rownum = 1 THEN 'Fail' ELSE
T1.studentsubjectstatus END AS studentperformance
FROM CTE AS T1
JOIN CTE2 AS T2 ON T1.studentname=T2.studentname AND T1.rownum=t2.maxrow
source to share
Try the following:
SELECT
studentName AS 'Student Name'
, CASE
WHEN Merit >= 2
THEN 'Merit'
WHEN Pass >= 2
THEN 'Pass'
ELSE 'Fail'
END AS 'Student Performance'
(SELECT
studentName
, COUNT(CASE WHEN StudentSubjectStatus = 'Merit' THEN 1 ELSE 0 END) AS 'Merit'
, COUNT(CASE WHEN StudentSubjectStatus = 'Pass' THEN 1 ELSE 0 END) AS 'Pass'
-- counting how many subjects the student failed is not needed,
-- but for example purposes it has been added
, COUNT(CASE WHEN StudentSubjectStatus = 'Fail' THEN 1 ELSE 0 END) AS 'Fail'
FROM Students
GROUP BY studentName)
source to share
you can do it using the aggregate case like below:
SELECT studentname,
CASE
WHEN Sum(CASE
WHEN studentsubjectstatus = 'Merit' THEN 1
ELSE 0
END) > 1 THEN 'Merit'
WHEN Sum(CASE
WHEN studentsubjectstatus = 'pass' THEN 1
ELSE 0
END) > 1 THEN 'pass'
ELSE 'fail'
END
FROM yourtable
GROUP BY studentname
OR
SELECT studentname,
CASE
WHEN Sum(CASE
WHEN studentsubjectstatus = 'Merit' THEN 1
ELSE 0
END) > 1 THEN 'Merit'
ELSE
CASE
WHEN Sum(CASE
WHEN studentsubjectstatus = 'pass' THEN 1
ELSE 0
END) > 1 THEN 'pass'
ELSE 'fail'
END
END AS studentperformance
FROM yourtable
GROUP BY studentname
note: the > 1
condition can be changed to = 2
if you want to count the exact 2 lines with a specific one studentsubjectstatus
.
source to share
Assuming there are always three subjects per student, here's an idea.
For everyone studentname
:
-
Count each one
studentsubjectstatus
. -
Sort the results first in descending order, and then alphabetically
studentsubjectstatus
(so thatFail
becomes first in the case of links). -
Get the top result.
So, something like this:
SELECT DISTINCT
studentname,
studentperformance = (
SELECT TOP (1)
s.studentsubjectstatus
FROM
dbo.yourtable AS s
WHERE
s.studentname = t.studentname
GROUP BY
s.studentsubjectstatus
ORDER BY
COUNT(*) DESC,
s.studentsubjectstatus ASC
)
FROM
dbo.yourtable AS t
;
If you don't want to rely on the alphabetical order of the values in studentsubjectstatus
, you can make sure that Fail
comes first by replacing s.studentsubjectstatus ASC
with something like this:
CASE s.studentsubjectstatus WHEN 'Fail' THEN 1 ELSE 2 END ASC
source to share