我正在尝试创建一个学生数据库,但我遇到了给定的要求。
我得到了创建2个表的表,一个表是学生的注册号,科目总和,另一个表是Student_name,总和的排名。我可以理解,这里我们需要单独使用学生姓名,以后再组合使用,但是让我对学生科目感到震惊的部分可能会有所不同,这意味着如果学生编号:1包含3个科目,则意味着学生编号:2可能包含2个,而学生否:3个可能有5个,因此我们必须将排名排序
CREATE TABLE Student
(StudentID int, StudentName varchar(6), Details varchar(1));
INSERT INTO Student
(StudentID, StudentName, Details)
VALUES
(1, 'John', 'X'),
(2, 'Paul', 'X'),
(3, 'George', 'X'),
(4, 'Paul', 'X');
CREATE TABLE Subject
(SubjectID varchar(1), SubjectName varchar(7));
INSERT INTO Subject
(SubjectID, SubjectName)
VALUES
('M', 'Math'),
('E', 'English'),
('H', 'History');
CREATE TABLE Mark
(StudentID int, SubjectID varchar(1), MarkRate int);
INSERT INTO Mark
(StudentID, SubjectID, MarkRate)
VALUES
(1, 'M', 90),
(1, 'E', 100),
(2, 'M', 95),
(2, 'E', 70),
(3, 'E', 95),
(3, 'H', 98),
(4, 'H', 90),
(4, 'E', 100);
我需要2个输出 第一个是
ID |subjects |marks
----------------------------------------------------------
1 maths 98
1 science 87
1 social 88
2 maths 87
2 english 99
3 maths 96
3 evs 100
3 social 88
3 history 90
第二个表为
NO |name |total|rank
----------------------------------------------------------
1 xxx 123 1
2 yyy 456 2
3 zzz 789 3
我需要这样的输出才能输入n个条目
答案 0 :(得分:3)
1 st one
SELECT
A.StudentID AS [ID],
C.SubjectName AS [subjects],
B.MarkRate AS [marks]
FROM STUDENT A JOIN Mark B ON A.StudentID=B.StudentID JOIN Subject C ON C.SubjectID=B.SubjectID
2nd one
SELECT
A.StudentID AS [NO],
A.StudentName AS [name],
SUM(B.MarkRate) AS [total],
ROW_NUMBER() OVER(ORDER BY SUM(B.MarkRate) DESC) AS [rank]
FROM STUDENT A JOIN Mark B ON A.StudentID=B.StudentID JOIN Subject C ON C.SubjectID=B.SubjectID
GROUP BY A.StudentID,A.StudentName
ORDER BY [total] DESC
答案 1 :(得分:2)
我知道第一个查询列出了每个学生的分数
SELECT
m.StudentID as ID,
s.SubjectName as subjects,
m.MarkRate as marks
FROM
Mark m
INNER JOIN Subject s on m.SubjectID = m.SubjectID
ORDER BY
m.StudentID,
s.SubjectName
第二个查询给出学生的总得分及其排名。
SELECT
X.StudentID,
X.StudentName,
ROWNUMBER() OVER ( ORDER BY X.TotalMark desc) as Rank
FROM (
SELECT
m.StudentID,
s.StudentName,
sum(m.MarkRate) TotalMark
FROM
Mark m
INNER JOIN Student s on s.StudentID = m.StudentID
GROUP BY
m.StudentID,
s.StudentName
) X
ORDER BY X.TotalMark desc
答案 2 :(得分:2)
select StudentID ,SubjectName ,MarkRate from
#Mark a join #Subject b on a.SubjectID=b.SubjectID
输出
StudentID SubjectName MarkRate
1 Math 90
1 English 100
2 Math 95
2 English 70
3 English 95
3 History 98
4 History 90
4 English 100
第二次查询
with cte as
(
select a.StudentID,StudentName,sum(MarkRate)MarkRate from #Student a join #Mark B on a.StudentID=b.StudentID
group by a.StudentID,StudentName
)
select *,rank() over( order by MarkRate desc) as rn from cte
输出
StudentID StudentName MarkRate rn
3 George 193 1
4 Paul 190 2
1 John 190 2
2 Paul 165 4
答案 3 :(得分:2)
要获得带有主题名称的标记列表,很简单:
您需要使用JOIN
:
SELECT M.StudentId
,SU.SubjectName
,M.MarkRate
FROM Mark M
INNER JOIN Subject SU ON M.SubjectID = SU.SubjectID
要获得排名的总分,您需要使用GROUP BY
和RANK()
函数:
SELECT M.StudentId
,ST.StudentName
,SUM(MarkRate) Total
,RANK() OVER(ORDER BY SUM(MarkRate) ) Rank
--,RANK() OVER(ORDER BY SUM(MarkRate) DESC) Rank
FROM Mark M
INNER JOIN Student ST ON M.StudentId = ST.StudentId
GROUP BY M.StudentId
,ST.StudentName
答案 4 :(得分:1)
您可以尝试对两个输出使用以下查询。
SELECT
Student.StudentID as ID,
[Subject].SubjectName as subjects,
Mark.MarkRate as marks
FROM
Student
INNER JOIN Mark on Student.StudentID = Mark.StudentID
INNER JOIN [Subject] on [Subject].SubjectID = Mark.SubjectID
ORDER BY
Student.StudentID,
SubjectName
在下面查询第二个输出。正如您所说的,那些得分较低的人获得了等级1,否则您可以在下面的查询中将Order By Total Desc
放在得分较高的人中。
SELECT ID, StudentName, Total, Row_number() Over (Order BY Total) Ranks FROM(
SELECT Id, StudentName, SUM(marks) as Total FROM (
SELECT
Student.StudentID as ID,
[Subject].SubjectName as subjects,
Mark.MarkRate as marks,
Student.StudentName
FROM
Student
INNER JOIN Mark on Student.StudentID = Mark.StudentID
INNER JOIN [Subject] on [Subject].SubjectID = Mark.SubjectID
)Tot
Group By Id, StudentName
)Ranks