0
Date           Flight ID    Member ID   Seat Type   Seat Price  
2013-07-28     F71498           M69202          Business    RM 40.00

               F73591           M69202          First           RM 50.00
                                M69202          First           RM 50.00

               F71498           M37520          Business    RM 40.00
                                M69202          Business    RM 40.00
                                                            ===============
                                                            Total : ??
                                                            ===============

我试过下面的 SQL

SELECT 
    r.ReservationDate, r.FlightID, r.MemberID, s.SeatType, s.SeatPrice
FROM 
    flight f, reservation r, seat s, member m
WHERE 
    r.FlightID = f.FlightID 
    AND r.SeatID = s.SeatID 
    AND r.MemberID = m.MemberID 
    AND ReservationDate = '2013-07-28' 
    AND s.FlightID = f.FlightID

我可以按应有的方式生成上面的报告,但我需要帮助汇总总数SeatPrice.. 我可以知道如何计算总数吗?谢谢先进...=)

4

1 回答 1

0

如果我理解正确,您希望您的结果集如下所示,即最后一行具有总计列和总计值

Date           Flight ID    Member ID   Seat Type   Seat Price  
2013-07-28     F71498        M69202      Business    RM 40.00

2013-07-28     F73591        M69202      First       RM 50.00
2013-07-28     F73591        M69202      First       RM 50.00

2013-07-28     F71498        M37520      Business    RM 40.00
2013-07-28     F71498        M69202      Business    RM 40.00

                                          Total      RM 220.0  

你可以做

SELECT 
    r.ReservationDate, r.FlightID, r.MemberID, s.SeatType, s.SeatPrice
FROM 
    flight f, reservation r, seat s, member m
    INNER JOIN reservation r
       ON r.FlightID = f.FlightID 
    INNER JOIN seat s
       ON r.SeatID = s.SeatID 
    INNER JOIN member m
       ON r.MemberID = m.MemberID
WHERE ReservationDate = '2013-07-28' 
      AND s.FlightID = f.FlightID

UNION

SELECT '','','','Total',SUM(s.SeatPrice)
FROM
FROM 
    flight f, reservation r, seat s, member m
    INNER JOIN reservation r
       ON r.FlightID = f.FlightID 
    INNER JOIN seat s
       ON r.SeatID = s.SeatID 
    INNER JOIN member m
       ON r.MemberID = m.MemberID
WHERE ReservationDate = '2013-07-28' 
      AND s.FlightID = f.FlightID
于 2013-07-29T05:33:33.567 回答