Table 1
S_no Name Attempt Score Status
1 sam 1 25 Fail
2 sam 2 45 Pass
3 sam 3 12 Fail
4 Dante 1 56 Pass
5 Dante 2 78 Pass
6 jill 1 16 Fail
7 Jack 1 90 Pass
In the Above table . number of student passed = 2 and number od student Failed = 2.
status of the Final Attempt is taken in to Consideration .
I want to Show
Id Passed_no failed_no
1 2 2
答案 0 :(得分:3)
您可以使用ROW_NUMBER()
和条件聚合:
SELECT COUNT(CASE WHEN s.status = 'Pass' THEN 1 END) as Passed_no,
COUNT(CASE WHEN s.status = 'Fail' THEN 1 END) as Failed_no
FROM (
SELECT t.*,
ROW_NUMBER() OVER(PARTITION BY t.name ORDER BY t.attempt DESC) as rnk
FROM YourTable t) s
WHERE s.rnk = 1
答案 1 :(得分:0)
您可以使用PIVOT获得结果:
SELECT *
FROM (
SELECT NAME ,CASE SUM(CASE WHEN STATUS='Pass' THEN 0 ELSE 1 END)
WHEN 0 THEN 'PASS'
ELSE 'FAIL'
END STATUS
FROM TABLE_NAME
GROUP BY NAME)
PIVOT (COUNT(NAME) FOR STATUS IN ('PASS' AS "Passed_no" ,'FAIL' AS "Failed_no"));