對於大片段代碼的道歉,但我需要顯示它們以顯示我已編譯的相關調查。結合查詢顯示不正確的結果
下面我有兩個單獨的查詢,從分期架構(Staging.SabreAssignedCrew
)表和其他特定的飛行機組成員的一個檢查是對數據的比較模式中的表,其中連接所有相關表格後,在一起,我得到每次飛行機組人員信息:
Staging.SabreAssignedCrew
表
SELECT
*
, Airline + CAST(FlightNumber AS VARCHAR) + Suffix AS StagingFlight
, ROW_NUMBER() OVER(PARTITION BY Airline + CAST(FlightNumber AS VARCHAR) + Suffix ORDER BY UpdateId DESC) AS StageRowNumber
FROM Staging.SabreAssignedCrew
WHERE Airline + CAST(FlightNumber AS VARCHAR) + Suffix = 'LS627' AND DepartureDate = '2016-09-04'
個
Data
模式表
SELECT
cd.CrewUpdateID
, cd.IsPassive
, cd.CrewBase
, cd.CrewCategory
, cd.CrewType
, cd.EmployeeNumber
, cd.FirstName
, cd.LastName
, c.UpdateID
, c.LegKey
, c.UpdateReceived
, DataFlight
, l.ScheduledDepartureDate
, l.ScheduledDepartureAirport
FROM
(SELECT *, ROW_NUMBER() OVER(PARTITION BY LegKey ORDER BY UpdateID DESC) AS RowNumber FROM Data.Crew) c
INNER JOIN
Data.CrewDetail cd
ON c.UpdateID = cd.CrewUpdateID
AND cd.IsPassive = 0
AND RowNumber = 1
INNER JOIN
(SELECT *, Carrier + CAST(FlightNumber AS VARCHAR) + Suffix AS DataFlight FROM Data.Leg) l
ON c.LegKey = l.LegKey
WHERE DataFlight = 'LS627' AND ScheduledDepartureDate = '2016-09-04'
兩個查詢的結果顯示,有6名機組人員(6行)在飛行:
現在我所要做的將查詢合併爲一個使用並執行SUM以確保正確的船員在航班上。然而,結果是不正確的,因爲它下面outputd:
我們知道,在飛行6名機組人員,因此它應該顯示爲分期模式2 StagingCabinCrew
和4 StagingCockpitCrew
完全一樣值爲數據架構。這全由CrewType
字段確定,這兩個字段來自分段和數據模式表。
我假設我在我的聯合查詢中做了一些錯誤,但我不確定它是如何在組合查詢中單獨運行上述兩個查詢來顯示正確的結果,但執行整個組合查詢顯示錯誤值。什麼需要解決?
WITH CTE AS (
SELECT
cd.CrewUpdateID
, cd.IsPassive
, cd.CrewBase
, cd.CrewCategory
, cd.CrewType
, cd.EmployeeNumber
, cd.FirstName
, cd.LastName
, c.UpdateID
, c.LegKey
, c.UpdateReceived
, DataFlight
, l.ScheduledDepartureDate
, l.ScheduledDepartureAirport
FROM
(SELECT *, ROW_NUMBER() OVER(PARTITION BY LegKey ORDER BY UpdateID DESC) AS RowNumber FROM Data.Crew) c
INNER JOIN
Data.CrewDetail cd
ON c.UpdateID = cd.CrewUpdateID
AND cd.IsPassive = 0
AND RowNumber = 1
INNER JOIN
(SELECT *, Carrier + CAST(FlightNumber AS VARCHAR) + Suffix AS DataFlight FROM Data.Leg) l
ON c.LegKey = l.LegKey
WHERE DataFlight = 'LS627' AND ScheduledDepartureDate = '2016-09-04'
)
SELECT
StagingFlight
, sac.DepartureDate
, sac.DepartureAirport
, cte.DataFlight
, cte.ScheduledDepartureDate
, cte.ScheduledDepartureAirport
, SUM(CASE WHEN sac.CREWTYPE = 'F' THEN 1 ELSE 0 END) AS StagingCabinCrew
, SUM(CASE WHEN sac.CREWTYPE = 'C' THEN 1 ELSE 0 END) AS StagingCockpitCrew
, SUM(CASE WHEN cte.CrewType = 'F' THEN 1 ELSE 0 END) AS DataCabinCrew
, SUM(CASE WHEN cte.CrewType = 'C' THEN 1 ELSE 0 END) AS DataCockpitCrew
FROM
(
SELECT
*
, Airline + CAST(FlightNumber AS VARCHAR) + Suffix AS StagingFlight
, ROW_NUMBER() OVER(PARTITION BY Airline + CAST(FlightNumber AS VARCHAR) + Suffix ORDER BY UpdateId DESC) AS StageRowNumber
FROM Staging.SabreAssignedCrew--
WHERE Airline + CAST(FlightNumber AS VARCHAR) + Suffix = 'LS627' AND DepartureDate = '2016-09-04')
sac
LEFT JOIN CTE cte
ON StagingFlight = DataFlight
AND sac.DepartureDate = cte.ScheduledDepartureDate
AND sac.DepartureAirport = cte.ScheduledDepartureAirport
AND sac.CREWTYPE = cte.CrewType
WHERE StagingFlight = 'LS627' AND DepartureDate = '2016-09-04' AND StageRowNumber = 1
GROUP BY
StagingFlight
, sac.DepartureDate
, sac.DepartureAirport
, cte.DataFlight
, cte.ScheduledDepartureDate
, cte.ScheduledDepartureAirport
在begining試着讓你的QRY更容易 - 「航空+ CAST(FLIGHTNUMBER AS VARCHAR)+後綴」可以計算持久列 - 將更具可讀性 - 由StagingFlight創建船員agregation(組出發日期,sac.DepartureAirport)到臨時表或查看 - 你不需要在你的qry中使用row_number(group by will be enough) – Deadsheep39
@ deadsheep39你想堅持你的評論作爲答案,所以如果它的工作,我可以標記它?我知道如何做視圖或臨時表,但也可以包含一個供其他人看的例子。謝謝 – BruceyBandit