没有分组所有选择的SQL的余额到期

时间:2012-09-01 18:14:09

标签: sql postgresql group-by

我已经提供了我的表的摘要,并且我已经在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
;

2 个答案:

答案 0 :(得分:1)

以下是我更新的查询版本:

SELECT      
    s.student_sis_id AS "Student ID",        
    s.student_last_name AS Last,
    s.student_first_name AS First,
    SUM(r.reservation_fee_amount) AS "Total Fees",
    COUNT(r.reservation_id) AS "Reservation Count",
    COALESCE(
        SUM(
            p.payment_check_amount
            + p.payment_cash_amount
            + p.payment_credit_card_amount
        ), 0.00
    ) AS "Amount Paid",
    SUM(r.reservation_fee_amount) - (
        COALESCE(
            SUM(
                p.payment_check_amount
                + p.payment_cash_amount
                + p.payment_credit_card_amount
            ), 0.00
        )  
    ) AS "Amount Due" 
FROM 
    student s
    INNER JOIN reservation r ON s.student_id = r.student_id  
    LEFT JOIN payment p ON p.student_id = r.student_id AND p.term_id = r.term_id
WHERE r.term_id IN (1,2)
GROUP BY
    s.student_id, 
    s.student_sis_id,        
    s.student_last_name,
    s.student_first_name
ORDER BY 
    s.student_sis_id
;

值得关注的事情:

  • 我在主(外部)查询中包含payments以避免子查询

  • 联接类型为LEFT [OUTER] JOIN,因此缺少任何payment行不会阻止其他数据出现在结果集中

  • 加入条件包括term_id(基本上这就是你迷路的地方,我认为)

  • 最后我使用短表别名来提高可读性。

我希望这就是你所追求的目标。

答案 1 :(得分:1)

找到2付款条目问题的解决方案(我原来的问题中没有认出)。这是答案:

set search_path to temp, public;
SELECT      
s.student_sis_id AS "Student ID",        
s.student_last_name AS "Last Name",
s.student_first_name AS "First Name",
SUM(r.reservation_fee_amount) AS "Total Fees",
COALESCE(p.paid, 0.00) AS "Amount Paid",
COALESCE(SUM(r.reservation_fee_amount) - p.paid, 0.00) AS "Amount Due"    
FROM 
    student s       
    INNER JOIN reservation r ON s.student_id = r.student_id
    left outer join 
    (
      select student_id, term_id,
        SUM(
            p.payment_check_amount
            + p.payment_cash_amount
            + p.payment_credit_card_amount
        ) AS "paid" 
      from payment p
      group by student_id, term_id
    ) as p 
    ON p.student_id = r.student_id AND p.term_id = r.term_id
        WHERE r.reservation_completed  AND  r.term_id IN (1,2)                      
GROUP BY
    s.student_sis_id,        
    s.student_last_name,
    s.student_first_name,
    p.paid
ORDER BY 
    s.student_sis_id

谢谢你dezso和davek

相关问题