需要以用户数量为先来拆分入口点
Need to split entry points with the number of user comes first
+------------+-------+---------+---------------+
| STudent_ID | Marks | Subject | EntryPoints |
+------------+-------+---------+---------------+
| 1 | 50 | Maths | 10 |
| 2 | 50 | Maths | 10 |
| 3 | 45 | Maths | 10 |
| 1 | 30 | History | 20 |
| 2 | 30 | History | 20 |
| 3 | 30 | History | 20 |
+------------+-------+---------+---------------+
预期输出:
+------------+-------+---------+---------------+
| student_id | Marks | Subject | TotalPoints |
+------------+-------+---------+---------------+
| 1 | 50 | Maths | 5 |
| 2 | 50 | Maths | 5 |
| 1 | 30 | History | 6.66 |
| 2 | 30 | History | 6.66 |
| 3 | 30 | History | 6.66 |
+------------+-------+---------+---------------+
总分计算
对于数学,入门分数为 10,得分最高的学生人数为 2,因此 10/2 = 5
对于历史,入学分数是 20,学生的最高分是 3,所以 20/3 = 6.66
我试过的查询:
select student_id,marks,subject from
(
select student_id,marks,subject,dense_rank() over ( partition by subject order by marks desc) rn from test
) t
where rn=1
输出:
+------------+-------+---------+
| Student_id | Marks | Subject |
+------------+-------+---------+
| 1 | 50 | Maths |
| 2 | 50 | Maths |
| 1 | 30 | History |
| 2 | 30 | History |
| 3 | 30 | History |
+------------+-------+---------+
我不知道如何在查询中获取总分列
我假设最高分是同一主题所有分数中的最高分。
SELECT
Student_ID
,Marks
,T.Subject
,CONVERT(decimal(18, 2),
CONVERT(float, EntryPoints)/
CONVERT(float, COUNT(*) OVER(PARTITION BY T.Subject))) as 'TotalPoints'
FROM @T T
INNER JOIN (SELECT DISTINCT Subject, MAX(Marks) OVER(PARTITION BY Subject)
as Max_Marks FROM @T) Scores
ON T.Subject = Scores.Subject
WHERE Marks = Scores.Max_Marks
ORDER BY Marks DESC, Student_ID
您可以在查询中使用 Group by
来获取主题的计数,然后 join
使用相同的查询获得结果。
下一个:-
Select a.student_id,a.marks,a.subject,
Cast(a.EntryPoints as decimal ) / cast(b.CountPerSubject as decimal) TotalPoints
from
(
select student_id,marks,subject ,EntryPoints
from
(
select student_id,marks,
subject, EntryPoints,
dense_rank() over ( partition by subject order by marks desc) rn
from test
) t
where rn=1
) a
Left Join
(
select subject, count(subject) CountPerSubject
from
(
select student_id,marks,subject ,EntryPoints
from
(
select student_id,marks,
subject, EntryPoints,
dense_rank() over ( partition by subject order by marks desc) rn
from test
) t
where rn=1 ) c
group by subject) b
on a.subject = b.subject
+------------+-------+---------+---------------+
| STudent_ID | Marks | Subject | EntryPoints |
+------------+-------+---------+---------------+
| 1 | 50 | Maths | 10 |
| 2 | 50 | Maths | 10 |
| 3 | 45 | Maths | 10 |
| 1 | 30 | History | 20 |
| 2 | 30 | History | 20 |
| 3 | 30 | History | 20 |
+------------+-------+---------+---------------+
预期输出:
+------------+-------+---------+---------------+
| student_id | Marks | Subject | TotalPoints |
+------------+-------+---------+---------------+
| 1 | 50 | Maths | 5 |
| 2 | 50 | Maths | 5 |
| 1 | 30 | History | 6.66 |
| 2 | 30 | History | 6.66 |
| 3 | 30 | History | 6.66 |
+------------+-------+---------+---------------+
总分计算
对于数学,入门分数为 10,得分最高的学生人数为 2,因此 10/2 = 5 对于历史,入学分数是 20,学生的最高分是 3,所以 20/3 = 6.66
我试过的查询:
select student_id,marks,subject from
(
select student_id,marks,subject,dense_rank() over ( partition by subject order by marks desc) rn from test
) t
where rn=1
输出:
+------------+-------+---------+
| Student_id | Marks | Subject |
+------------+-------+---------+
| 1 | 50 | Maths |
| 2 | 50 | Maths |
| 1 | 30 | History |
| 2 | 30 | History |
| 3 | 30 | History |
+------------+-------+---------+
我不知道如何在查询中获取总分列
我假设最高分是同一主题所有分数中的最高分。
SELECT
Student_ID
,Marks
,T.Subject
,CONVERT(decimal(18, 2),
CONVERT(float, EntryPoints)/
CONVERT(float, COUNT(*) OVER(PARTITION BY T.Subject))) as 'TotalPoints'
FROM @T T
INNER JOIN (SELECT DISTINCT Subject, MAX(Marks) OVER(PARTITION BY Subject)
as Max_Marks FROM @T) Scores
ON T.Subject = Scores.Subject
WHERE Marks = Scores.Max_Marks
ORDER BY Marks DESC, Student_ID
您可以在查询中使用 Group by
来获取主题的计数,然后 join
使用相同的查询获得结果。
下一个:-
Select a.student_id,a.marks,a.subject,
Cast(a.EntryPoints as decimal ) / cast(b.CountPerSubject as decimal) TotalPoints
from
(
select student_id,marks,subject ,EntryPoints
from
(
select student_id,marks,
subject, EntryPoints,
dense_rank() over ( partition by subject order by marks desc) rn
from test
) t
where rn=1
) a
Left Join
(
select subject, count(subject) CountPerSubject
from
(
select student_id,marks,subject ,EntryPoints
from
(
select student_id,marks,
subject, EntryPoints,
dense_rank() over ( partition by subject order by marks desc) rn
from test
) t
where rn=1 ) c
group by subject) b
on a.subject = b.subject