私は自分のテーブルの要約を提供し、SQLをうまく使い始めましたが、返されるアイテムの数を制限する方法を理解することに固執しています。1つ以上の用語を選択し、それらの用語だけで残高を取り戻すことができるはずです。
学生は1つのレコードを持っている必要があり、複数の条件で複数の予約を行うことができますが、支払いは予約に固有のものではなく、学生に固有のものです。それが私を失望させている部分です。
テーブルの構造、日付、SQLの開始は次のとおりです。誰かが私を助けてくれますか?この結果は、SueSmithの第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
;