SQL 服务器中带有查询结果的新列
New Column with query result in SQL Server
我需要添加带有查询结果的新列。
我有这个查询:
SELECT DISTINCT Arrival
, Flight
, TotalPax.SumPassengers
, TotalPaxLocal.SumLocalPassengers
, STD
, STA
--, PassengerID
--, Departure
--, JourneyNumber
--, SegmentNumber
--, LegNumber
--, InventoryLegKey
--, RecordLocator
FROM #TempLocalOrg tmp
CROSS APPLY
(
SELECT COUNT(1) AS SumPassengers
FROM #TempLocalOrg crossTemp
WHERE tmp.Flight = crossTemp.Flight
) TotalPax
CROSS APPLY
(
SELECT COUNT(1) AS SumLocalPassengers
FROM #TempLocalOrg crossTemp
WHERE tmp.Flight = crossTemp.Flight
AND tmp.Arrival = crossTemp.Arrival
AND tmp.SegmentNumber = 1
AND tmp.LegNumber = 1
) TotalPaxLocal
TotalPax 正在运行,但是 TotalPaxLocal 不运行,我需要交叉应用或类似下面的查询:
SELECT Count(*)
FROM #TempLocalOrg
WHERE Flight = 1500
AND Arrival = 'FLN'
AND SegmentNumber = 1
AND LegNumber = 1
所以,我需要为这个查询替换 Cross Apply TotalPaxLocal。
错误的输出:
Arrival | Flight | TotalPax | TotalPaxLocal | ETD | ETA
--------|--------|----------|---------------|------------------|----------------
FLN | 1500 | 144 | 144 | 05/22/2015 08:25 | 05/22/2015 09:35
正确的输出:
Arrival | Flight | TotalPax | TotalPaxLocal | ETD | ETA
--------|--------|----------|---------------|------------------|----------------
FLN | 1500 | 144 | 52 | 05/22/2015 08:25 | 05/22/2015 09:35
只需使用 window 函数进行这些计算:
SELECT DISTINCT tmp.Arrival, tmp.Flight,
COUNT(*) OVER (PARTITION BY Flight) as NumPassengers,
SUM(CASE WHEN SegmentNumber = 1 AND LegNumber = 1 THEN 1 ELSE 0 END) OVER
(PARTITION BY Flight, Arrival)
) as NumLocalPassengers,
STD, STA
FROM #TempLocalOrg tmp;
我需要添加带有查询结果的新列。
我有这个查询:
SELECT DISTINCT Arrival
, Flight
, TotalPax.SumPassengers
, TotalPaxLocal.SumLocalPassengers
, STD
, STA
--, PassengerID
--, Departure
--, JourneyNumber
--, SegmentNumber
--, LegNumber
--, InventoryLegKey
--, RecordLocator
FROM #TempLocalOrg tmp
CROSS APPLY
(
SELECT COUNT(1) AS SumPassengers
FROM #TempLocalOrg crossTemp
WHERE tmp.Flight = crossTemp.Flight
) TotalPax
CROSS APPLY
(
SELECT COUNT(1) AS SumLocalPassengers
FROM #TempLocalOrg crossTemp
WHERE tmp.Flight = crossTemp.Flight
AND tmp.Arrival = crossTemp.Arrival
AND tmp.SegmentNumber = 1
AND tmp.LegNumber = 1
) TotalPaxLocal
TotalPax 正在运行,但是 TotalPaxLocal 不运行,我需要交叉应用或类似下面的查询:
SELECT Count(*)
FROM #TempLocalOrg
WHERE Flight = 1500
AND Arrival = 'FLN'
AND SegmentNumber = 1
AND LegNumber = 1
所以,我需要为这个查询替换 Cross Apply TotalPaxLocal。
错误的输出:
Arrival | Flight | TotalPax | TotalPaxLocal | ETD | ETA
--------|--------|----------|---------------|------------------|----------------
FLN | 1500 | 144 | 144 | 05/22/2015 08:25 | 05/22/2015 09:35
正确的输出:
Arrival | Flight | TotalPax | TotalPaxLocal | ETD | ETA
--------|--------|----------|---------------|------------------|----------------
FLN | 1500 | 144 | 52 | 05/22/2015 08:25 | 05/22/2015 09:35
只需使用 window 函数进行这些计算:
SELECT DISTINCT tmp.Arrival, tmp.Flight,
COUNT(*) OVER (PARTITION BY Flight) as NumPassengers,
SUM(CASE WHEN SegmentNumber = 1 AND LegNumber = 1 THEN 1 ELSE 0 END) OVER
(PARTITION BY Flight, Arrival)
) as NumLocalPassengers,
STD, STA
FROM #TempLocalOrg tmp;