如何在ORACLE中计算总计?

时间:2014-08-04 03:27:38

标签: sql oracle

SELECT f.flight_id,
       COUNT(b.Booking_ID) As "Total SeatPurchased",
       COUNT(b.Booking_ID)*flight_price 
FROM   booking b , flight f
WHERE  b.flight_id=f.flight_id 
GROUP BY f.flight_id,f.flight_price
ORDER BY COUNT(b.booking_id) desc;

如何计算此sql的总计?我想计算所有的flightPrice和整个事情的结尾。我想把所有的总结作为总计。

示例输出:

FLIGHT_ID  Total Seat Purchased  COUNT(B.BOOKING_ID)*FLIGHT_PRICE 
-----------------------------------------------------------------
F0006                       149                            11771                
F0011                       148                            38332                
F0012                       131                            11528                
F0003                       130                            33670                
F0010                       115                            10120                
F0004                       113                             8362                
F0005                       108                             7992                
F0007                       101                             7373                
F0015                        80                            11200                
F0002                        50                             5700                
F0008                        41                             8774                
F0013                        36                             8316                
F0001                        35                             6265                
F0014                        35                             7490                
F0009                        35                             4690  
                                               Grandtotal:xxxxxx  

3 个答案:

答案 0 :(得分:0)

然后试试这个:

  

COMPUNT SUM OF COUNT(b.Booking_ID)* flight_price ON REPORT

SELECT f.flight_id,COUNT(b.Booking_ID) As "Total SeatPurchased", COUNT(b.Booking_ID)*flight_price 
FROM booking b , flight f
WHERE b.flight_id=f.flight_id 
--    GROUP BY ROLLUP ( f.flight_id,f.flight_price )
GROUP BY  f.flight_id,f.flight_price 
ORDER BY COUNT(b.booking_id) DESC;

答案 1 :(得分:0)

要获得授权价值,请使用此:

    SUM(COUNT(B.BOOKING_ID)*FLIGHT_PRICE)

答案 2 :(得分:0)

我认为这可能适合你:

SELECT f.flight_id,COUNT(b.Booking_ID) As "Total SeatPurchased",
       COUNT(b.Booking_ID)*flight_price, 
       (SELECT SUM(FlightPrice)
          FROM (  SELECT f.flight_id,
                         COUNT(b.Booking_ID) As "Total SeatPurchased",
                         COUNT(b.Booking_ID)*flight_price As FlightPrice 
                    FROM booking b , flight f
                   WHERE b.flight_id=f.flight_id 
                GROUP BY f.flight_id,f.flight_price)) As "Grand Total"
 FROM booking b , flight f
WHERE b.flight_id=f.flight_id 
GROUP BY f.flight_id,f.flight_price
ORDER BY COUNT(b.booking_id) desc;

我目前没有使用Oracle,因此无法对其进行测试。我非常确定这不会非常有效,所以如果可能的话,你可能要考虑从调用代码中计算总计。

更新1

(SELECT f.flight_id,COUNT(b.Booking_ID) As "Total SeatPurchased",
       COUNT(b.Booking_ID)*flight_price
 FROM booking b , flight f
WHERE b.flight_id=f.flight_id 
GROUP BY f.flight_id,f.flight_price
ORDER BY COUNT(b.booking_id) desc)
UNION
SELECT null, 
       null, 
       SUM(FlightPrice)
  FROM (  SELECT f.flight_id,
                 COUNT(b.Booking_ID) As "Total SeatPurchased",
                 COUNT(b.Booking_ID)*flight_price As FlightPrice 
            FROM booking b , flight f
           WHERE b.flight_id=f.flight_id 
        GROUP BY f.flight_id,f.flight_price)

我认为这就是你所追求的目标,但将总计作为额外的行添加似乎并不是一个好主意。我仍然建议从主叫代码中计算出总计。