使用Max()和Sum()

时间:2017-11-03 09:53:22

标签: mysql

我有两张桌子:一张用于货物详情,另一张用于航班详情:

image description

我需要找到那些打架最重的航空公司的名字。 我尝试使用子查询解决它,但我无法得到结果 我的问题:

SELECT AIRLINE_NAME 
FROM FLIGHT 
WHERE EXISTS (SELECT MAX(TOTAL_WEIGHT) 
              FROM (SELECT SUM(WEIGHT_IN_KG)AS TOTAL_WEIGHT 
                    FROM CARGO 
                    GROUP BY FLIGHT_NO)
              );

有人可以帮助我了解如何一起使用Max()Sum()吗?

4 个答案:

答案 0 :(得分:0)

试试这个 -

SELECT AIRLINE_NAME 
FROM FLIGHT 
WHERE (FLIGHT_NO) = (SELECT FLIGHT_NO 
                     FROM (SELECT FLIGHT_NO, MAX(TOTAL_WEIGHT) 
                           FROM (SELECT FLIGHT_NO, SUM(WEIGHT_IN_KG) AS TOTAL_WEIGHT 
                                 FROM CARGO 
                                 GROUP BY FLIGHT_NO
                                ) X
                          ) Y
                     );

答案 1 :(得分:0)

select WEIGHT_IN_KG,t2.FLIGHT_NO,
(select AIRLINE_NAME from FLIGHT where FLIGHT.FLIGHT_NO = t2.FLIGHT_NO) AIRLINE_NAME

from 
(select MAX(WEIGHT_IN_KG) WEIGHT_IN_KG,FLIGHT_NO from 
(select SUM(WEIGHT_IN_KG) as WEIGHT_IN_KG,FLIGHT_NO from CARGO group by FLIGHT_NO) t1) t2

根据您的建议,我尝试在Subquery中对其进行编码,这可能对您有所帮助。

答案 2 :(得分:0)

select top 1 FLIGHT.AIRLINE_NAME from CARGO 
inner join FLIGHT on FLIGHT.FLIGHT_NO = CARGO.FLIGHT_NO GROUP BY FLIGHT.AIRLINE_NAME order by sum(CARGO.WEIGHT_IN_KG) desc

这将获取携带最重包裹的航班名称。

答案 3 :(得分:-1)

select FLIGHT.FLIGHT_NO,FLIGHT.AIRLINE_NAME,Max(CARGO.WEIGHT_IN_KG) WEIGHT_IN_KG from CARGO 
inner join FLIGHT on FLIGHT.FLIGHT_NO = CARGO.FLIGHT_NO
group by FLIGHT_NO,AIRLINE_NAME limt 1;

这可以帮到你。