我需要将列值与另一列值相乘,然后找到总和
SubjectTable
SubjectMarksId SubjectCode Marks
1 4ww 50
2 8qw 50
3 7uy 50
MarksType
MarksTypeId MarksType StudentCount Marks
1 Sports 1 10
2 Math 1 10
3 English 1 1
4 Physics 1 10 // Marks sum(10+10+1*50+10) multiply condition, where MarksTypeId=3 with SubjectCode = 8qw in SubjectTable
这是我试图成倍增加的方式
select StudentMark*(SELECT Marks AS SubjectMark
FROM SubjectTable
WHERE SubjectCode = '8qw')
from
(
SELECT Marks AS StudentMark
FROM MarksType
WHERE MarksTypeId= '3'
) src
最后是总和,它工作正常,但如何乘以另一列值然后找到总和?
SELECT Student.StudentId, Student.Name, sum(MarksType.Marks) as TotalMarks
FROM
MarksType
INNER JOIN
MarksDetails on MarksType.MarksTypeId = MarksDetails.MarksTypeId
INNER JOIN
Student on Student.StudentId = MarksDetails.StudentId
group by Student.Name,Student.StudentId
任何帮助都会很棒。
UPDATE:
由于情况原因,表格细节较少,所以输出错误,无论如何感谢@Darka和Piyush。下一次从我这边的细节将以明确的方式提供,如果要求我可以通过版主的帮助删除这个问题。这可能会影响其他人的研究。
答案 0 :(得分:0)
你可以在SUM函数中进行数学运算,即
SUM(Column1 * Column2) as Result
答案 1 :(得分:0)
你可以这样做:
SELECT SUM(MT.Marks * ISNULL(T.Marks,1))
FROM MarksType AS MT
OUTER APPLY (
SELECT ST.Marks
FROM SubjectTable AS ST
WHERE ST.SubjectCode = '8qw' AND MT.MarksTypeId= 3
) AS T(Marks)
最后SUM
可以是这样的(只是猜测没有提供数据):
SELECT Student.StudentId, Student.Name, sum(MT.Marks * ISNULL(T.Marks,1)) as TotalMarks
FROM MarksType AS MT
OUTER APPLY (
SELECT ST.Marks
FROM SubjectTable AS ST
WHERE ST.SubjectCode = '8qw' AND MT.MarksTypeId= 3
) AS T(Marks)
INNER JOIN MarksDetails
on MT.MarksTypeId = MarksDetails.MarksTypeId
INNER JOIN Student
on Student.StudentId = MarksDetails.StudentId
group by Student.Name,Student.StudentId