用SQL计算百分比

用SQL计算百分比,sql,Sql,我有一个名为ExamResult的表,它有以下列 studentId, Subject, Standard, Marks studentName(from student table),Standard,Percentage(with highest percentage on top) 我的表中有以下值 对于studentId=1 1,maths,9,78 1,english,9,80 1,history,9,67 1,geography,9,90 对于学生ID=2 2,maths,9,

我有一个名为
ExamResult
的表,它有以下列

studentId, Subject, Standard, Marks
studentName(from student table),Standard,Percentage(with highest percentage on top)
我的表中有以下值

对于studentId=1

1,maths,9,78
1,english,9,80
1,history,9,67
1,geography,9,90
对于学生ID=2

2,maths,9,68
2,english,9,83
2,history,9,70
2,geography,9,69
类似的条目直到studentId 30

现在我想计算每个学生的百分比,并想从具有以下列的表中选择数据

studentId, Subject, Standard, Marks
studentName(from student table),Standard,Percentage(with highest percentage on top)
例如:

Amit,9,78%
Sam,9,77%
现在的问题是如何在SQL中计算这个百分比

select stu.name,exam.standard,(what to do here) as Percentage 
from Student stu 
inner join ExamResult exam
on stu.Id=exam.studentId;
请帮我试试:

SELECT stu.name, exam.standard, AVG(stu.marks) as 'Percentage'
FROM Student stu 
INNER JOIN ExamResult exam
    ON stu.Id=exam.studentId;
GROUP BY stu.name, exam.standard

注意:这偏向于Microsoft SQL Server的SQL风格。您没有指定正在使用的版本。希望您能从这里开始工作。

您使用过GROUPBY子句吗

select stu.name,exam.standard, **average(marks) as Percentage** 
from Student stu 
inner join ExamResult exam
on stu.Id=exam.studentId
**group by stendent_id
order by Percentage  desc**

尝试使用AVERAGE函数来平均分数:

select stu.name,exam.standard,AVERAGE(marks) + '%'  as Percentage 
from Student stu 
inner join ExamResult exam
on stu.Id=exam.studentId
group by stu.name, exam.standard;

分组方式将每个学生/考试对视为一个单独的行,这样每个学生/考试组合的结果将有一行,并且将平均每个学生/考试组合的分数。

而不是向下投票,如果您能帮助我或让我知道我哪里做错了,那就太好了查找聚合函数和
分组依据
。此外,在发布sql问题时,您应该包括您正在使用的DBMS。您正在使用哪些DBMS?博士后?甲骨文?你对百分比的定义是什么,如何计算?一个学生获得的总分乘以400,再乘以100,计算出%谢谢你的回答,不能因为你没有足够的声誉而投票给你。你能不能再告诉我一件事,我怎样才能在我的记录中添加%符号?我建议用代码来做,但是如果您必须使用SQL:CAST(AVG(stu.marks)作为VARCHAR)+“%”作为“Percentage”来执行此操作,请感谢您的回答和解释,它清除了很多问题。不能因为你没有足够的声誉而高估你。你能再告诉我一件事吗?我怎样才能在我的记录中添加%符号?@PrachiJain,这只是串联。在大多数db供应商中,这将是a+。在甲骨文中是| |。例:平均分(分数)+“%”将在你的平均分后加上一个百分比。如果不起作用,请告诉我您使用的数据库平台。感谢您的回答,不能将其标记为答案,但我这边的a+1帮助了我。:)