含总费用的学生证

Student ID with total fees

Write a query to display the student's ID and the total fees paid by each student. Give alias name to total fees as TOTAL FEES. Sort the result based on student ID.

它有三个表

  1. 课程

    库伊德 课程名 期间 费用

  2. 学生

    学生 名 姓 街道 城市 出生日期

  3. 注册

    课程编号 学生 司法部

我已经执行了程序

Select studid,
    (count(courseid)*fees) as total fees
From student
Join registration using(studid)
Join course using (courseid)
Group by studid,fees
Order by studid;

预期结果:

Studid      totalfees
3001        4000
3002        4000
3003        4000
3004        19000
3005        18000
3006        4000
3007        3000
3008        7000
3009        10000

你只需要使用SUM聚合函数如下:

SELECT
    STUDID,
    SUM(FEES) AS "TOTAL FEES"
FROM
    REGISTRATION
    JOIN COURSE USING ( COURSEID )
GROUP BY
    STUDID
ORDER BY
    STUDID;

注意:您甚至可以跳过在查询中使用STUDENT table。