我需要添加包含查询结果的新列。
我有这个查询:
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无法正常工作,我需要Cross Apply或类似下面的查询:
SELECT Count(*)
FROM #TempLocalOrg
WHERE Flight = 1500
AND Arrival = 'FLN'
AND SegmentNumber = 1
AND LegNumber = 1
因此,我需要为此查询替换Cross Apply TotalPaxLocal。
错误的OutPut:
Arrival | Flight | TotalPax | TotalPaxLocal | ETD | ETA
--------|--------|----------|---------------|------------------|----------------
FLN | 1500 | 144 | 144 | 05/22/2015 08:25 | 05/22/2015 09:35
纠正OutPut:
Arrival | Flight | TotalPax | TotalPaxLocal | ETD | ETA
--------|--------|----------|---------------|------------------|----------------
FLN | 1500 | 144 | 52 | 05/22/2015 08:25 | 05/22/2015 09:35
答案 0 :(得分:2)
只需使用窗口函数进行这些计算:
prompt()