我已经提供了我的表的摘要,并且我在 SQL 方面有了一个良好的开端,但我一直在弄清楚如何限制返回的项目数量。我应该能够选择一个或多个条款,并从这些条款中取回应得的余额。
一个学生应该有 1 条记录,他们可以在多个学期进行多次预订,但付款不是针对预订的,而是针对学生的。那是让我失望的部分。
下面是表结构、日期和我的 SQL 的开始。有人可以帮我吗?此结果不应显示 Sue Smith 从第 3 学期开始支付的 500 美元。
我正在使用 PostgreSQL,但我认为这是一个非常基本的问题,不需要任何特定于 Postgres 的东西。
当前结果集:
Student ID Last First Total Fees Reservation Count Amount Paid Amount Due
123456 Jones Amy 50 1 50 0
412365 Smith Sue 100 3 545 -445
741258 Anderson Jon 50 1 0.00 50.00
963258 Holmes Fred 100 2 30 70
架构:
SET search_path TO temp, public;
CREATE TABLE term
(term_id SERIAL PRIMARY KEY,
term_title VARCHAR(100));
CREATE TABLE student
(student_id SERIAL PRIMARY KEY,
student_sis_id VARCHAR(15),
student_first_name VARCHAR(30),
student_last_name VARCHAR(100));
CREATE TABLE reservation
(reservation_id SERIAL PRIMARY KEY,
student_id INTEGER REFERENCES student ON UPDATE CASCADE,
term_id INTEGER REFERENCES term ON UPDATE CASCADE,
reservation_fee_amount NUMERIC DEFAULT 0.00);
CREATE TABLE payment
(payment_id SERIAL PRIMARY KEY,
student_id INTEGER REFERENCES student ON UPDATE CASCADE,
term_id INTEGER REFERENCES term ON UPDATE CASCADE,
payment_cash_amount NUMERIC,
payment_credit_card_amount NUMERIC,
payment_check_amount NUMERIC);
INSERT INTO term VALUES (DEFAULT, 'SESSION 1');
INSERT INTO term VALUES (DEFAULT, 'SESSION 2');
INSERT INTO term VALUES (DEFAULT, 'SESSION 3');
INSERT INTO student VALUES (DEFAULT, 412365, 'Sue', 'Smith');
INSERT INTO student VALUES (DEFAULT, 123456, 'Amy', 'Jones');
INSERT INTO student VALUES (DEFAULT, 741258, 'Jon', 'Anderson');
INSERT INTO student VALUES (DEFAULT, 963258, 'Fred', 'Holmes');
INSERT INTO reservation VALUES (DEFAULT, 1, 1, 50);
INSERT INTO reservation VALUES (DEFAULT, 1, 2, 50);
INSERT INTO reservation VALUES (DEFAULT, 2, 1, 50);
INSERT INTO reservation VALUES (DEFAULT, 3, 2, 50);
INSERT INTO reservation VALUES (DEFAULT, 4, 1, 50);
INSERT INTO reservation VALUES (DEFAULT, 4, 2, 50);
INSERT INTO reservation VALUES (DEFAULT, 1, 3, 50);
INSERT INTO payment VALUES (DEFAULT, 1, 1, 25, 0, 0);
INSERT INTO payment VALUES (DEFAULT, 1, 1, 0, 20, 0);
INSERT INTO payment VALUES (DEFAULT, 2, 1, 25, 25, 0);
INSERT INTO payment VALUES (DEFAULT, 4, 1, 10, 10, 10);
INSERT INTO payment VALUES (DEFAULT, 1, 3, 500, 0, 0);
查询:
SELECT
student.student_sis_id AS "Student ID",
student.student_last_name AS Last,
student.student_first_name AS First,
SUM(reservation.reservation_fee_amount) AS "Total Fees",
(
SELECT COUNT(reservation.reservation_id)
FROM reservation
WHERE student.student_id = reservation.student_id
) AS "Reservation Count",
(
SELECT
COALESCE(SUM(
payment.payment_check_amount
+ payment.payment_cash_amount
+ payment.payment_credit_card_amount
), 0.00)
FROM payment
WHERE payment.student_id = student.student_id
) AS "Amount Paid",
SUM(reservation.reservation_fee_amount) - (
SELECT
COALESCE(SUM(
payment.payment_check_amount
+ payment.payment_cash_amount
+ payment.payment_credit_card_amount
), 0.00)
FROM payment WHERE payment.student_id = student.student_id
) AS "Amount Due"
FROM
student
INNER JOIN reservation ON student.student_id = reservation.student_id
WHERE reservation.term_id IN (1,2)
GROUP BY
student.student_id,
student.student_sis_id,
student.student_last_name,
student.student_first_name
ORDER BY
student.student_sis_id
;