简体   繁体   中英

SQL = SUM up the TOTAL

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 : ??
                                                            ===============

I've tried the following 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

I can generate the report above as it should, but i need help in SUM up the total SeatPrice .. May I know how to count the Total? Thx in advanced...=)

If I understand you correctly , you want your resultset like below ie with last row having column Total and value of total

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  

You can do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM