如何使 SUM() 仅对 MIN() 选择的乘客求和?
How do I make SUM() to only sum the passengers selected by the MIN()?
我有一个关于 SQL 服务器的问题。我试图获得航班数量最少的航空公司,我还必须获得 MIN()
选择的航空公司在其航班中的乘客总数。我编写了一个代码,允许我获取航空公司的 MIN()
和 Name
,但我无法以正确的方式获取总和,它总是总结所有内容。我将插入我的数据库表和我用来获取 MIN()
的代码。请记住,我不能使用显式值。
我的表:
FLIGHTS
NumFlight | Date | Origin |Destination| Airline | NumPassengers | International|
___________|_____________________|________|___________|___________|_______________|______________|
44 2014-02-08 2 3 3 135 Yes
76 2014-03-17 2 1 2 80 No
380 2014-06-04 5 1 2 147 No
421 2014-04-21 1 2 1 185 No
572 2014-09-12 5 4 3 99 Yes
906 2014-05-10 3 2 3 154 Yes
918 2014-12-24 1 5 1 122 No
AIRLINES
AirlineID | Name |
______________|___________|
1 Delta
2 United
3 Air France
CITY
CityID | City |
______________|_________________|
1 Chicago
2 New York
3 Paris
4 Madrid
5 Houston
我的查询:
SELECT A.Name, MINIMUM.[# of Flights] AS 'N. of Flights'
FROM AIRLINES A,
(SELECT MIN(F.Total) AS '# of Flights' FROM
(SELECT Airline, COUNT(Airline) AS 'Total' FROM FLIGHTS GROUP BY Airline) F,
AIRLINES A
WHERE A.AirlineID = F.Airline) MINIMUM,
(SELECT Airline, COUNT(Airline) AS 'Total' FROM FLIGHTS GROUP BY Airline) TOTAL
WHERE TOTAL.Total = MINIMUM.[# of Flights] AND A.AirlineID = TOTAL.Airline
您可以将 window 函数与 apply
一起使用:
select a.name, f1.Total_flights, TotalPassenger
from AIRLINES a inner join
(select Airline, count(*) as Total_flights,
row_number() over (partition by Airline order by count(*)) as seq
from FLIGHTS
group by Airline
) fl
on fl.Airline = a.AirlineID and seq = 1 cross apply
(select sum(f.NumPassengers) as TotalPassenger
from FLIGHTS f
where f.Airline = f1.Airline
) fpassenger;
I have a question about SQL Server. I'm trying to get the airlines with the least number of flights
我建议使用 top (1) with ties
:
select a.*, f.numflights, f.NumPassengers
from airlines a join
(select top (1) with ties airlineid, count(*) as numflights, sum(NumPassengers) as NumPassengers
from flights f
group by airlineid
order by count(*) asc
) f
on f.airlineid = a.airlineid;
我有一个关于 SQL 服务器的问题。我试图获得航班数量最少的航空公司,我还必须获得 MIN()
选择的航空公司在其航班中的乘客总数。我编写了一个代码,允许我获取航空公司的 MIN()
和 Name
,但我无法以正确的方式获取总和,它总是总结所有内容。我将插入我的数据库表和我用来获取 MIN()
的代码。请记住,我不能使用显式值。
我的表:
FLIGHTS
NumFlight | Date | Origin |Destination| Airline | NumPassengers | International|
___________|_____________________|________|___________|___________|_______________|______________|
44 2014-02-08 2 3 3 135 Yes
76 2014-03-17 2 1 2 80 No
380 2014-06-04 5 1 2 147 No
421 2014-04-21 1 2 1 185 No
572 2014-09-12 5 4 3 99 Yes
906 2014-05-10 3 2 3 154 Yes
918 2014-12-24 1 5 1 122 No
AIRLINES
AirlineID | Name |
______________|___________|
1 Delta
2 United
3 Air France
CITY
CityID | City |
______________|_________________|
1 Chicago
2 New York
3 Paris
4 Madrid
5 Houston
我的查询:
SELECT A.Name, MINIMUM.[# of Flights] AS 'N. of Flights'
FROM AIRLINES A,
(SELECT MIN(F.Total) AS '# of Flights' FROM
(SELECT Airline, COUNT(Airline) AS 'Total' FROM FLIGHTS GROUP BY Airline) F,
AIRLINES A
WHERE A.AirlineID = F.Airline) MINIMUM,
(SELECT Airline, COUNT(Airline) AS 'Total' FROM FLIGHTS GROUP BY Airline) TOTAL
WHERE TOTAL.Total = MINIMUM.[# of Flights] AND A.AirlineID = TOTAL.Airline
您可以将 window 函数与 apply
一起使用:
select a.name, f1.Total_flights, TotalPassenger
from AIRLINES a inner join
(select Airline, count(*) as Total_flights,
row_number() over (partition by Airline order by count(*)) as seq
from FLIGHTS
group by Airline
) fl
on fl.Airline = a.AirlineID and seq = 1 cross apply
(select sum(f.NumPassengers) as TotalPassenger
from FLIGHTS f
where f.Airline = f1.Airline
) fpassenger;
I have a question about SQL Server. I'm trying to get the airlines with the least number of flights
我建议使用 top (1) with ties
:
select a.*, f.numflights, f.NumPassengers
from airlines a join
(select top (1) with ties airlineid, count(*) as numflights, sum(NumPassengers) as NumPassengers
from flights f
group by airlineid
order by count(*) asc
) f
on f.airlineid = a.airlineid;