2016-11-17 130 views
1

對於大片段代碼的道歉,但我需要顯示它們以顯示我已編譯的相關調查。結合查詢顯示不正確的結果

下面我有兩個單獨的查詢,從分期架構(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行)在飛行:

enter image description here

現在我所要做的將查詢合併爲一個使用並執行SUM以確保正確的船員在航班上。然而,結果是不正確的,因爲它下面outputd:

enter image description here

我們知道,在飛行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 
+0

在begining試着讓你的QRY更容易 - 「航空+ CAST(FLIGHTNUMBER AS VARCHAR)+後綴」可以計算持久列 - 將更具可讀性 - 由StagingFlight創建船員agregation(組出發日期,sac.DepartureAirport)到臨時表或查看 - 你不需要在你的qry中使用row_number(group by will be enough) – Deadsheep39

+0

@ deadsheep39你想堅持你的評論作爲答案,所以如果它的工作,我可以標記它?我知道如何做視圖或臨時表,但也可以包含一個供其他人看的例子。謝謝 – BruceyBandit

回答

0
  1. 創建stagingflight列(你可能會使用apropriete指數)。請始終設置varchar的大小(例如varchar - > varchar(30))。

    alter table staging.sabreassignedcrew 
    add stagingflight as cast(airline + cast(flightnumber as varchar(5)) + suffix as varchar(25)) persisted 
    
  2. 創建視圖。

    CREATE VIEW Staging.vw_SabreAssignedCrewCount 
    AS 
    SELECT 
        StagingFlight, 
        DepartureDate, 
        COUNT(DISTINCT CASE WHEN sac.CREWTYPE = 'F' THEN EmployeeNumber END) AS StagingCabinCrewCnt, 
        COUNT(DISTINCT CASE WHEN sac.CREWTYPE = 'C' THEN EmployeeNumber END) AS StagingCockpitCrewCnt 
    FROM Staging.SabreAssignedCrew 
    GROUP BY StagingFlight, DepartureDate 
    

用於第二表中的第二視圖。

  1. 你不需要rownumbers,CTE。只有輕鬆加入2個分組選擇。
+0

感謝Deadsheep39爲你的答案,遺憾的是我發現我有限制創建視圖或更改表的權限,所以我將不得不現在使用CTE。在過去的兩個小時裏我一直試圖把它整理出來,但仍然沒有運氣。您是否瞭解問題出在哪裏,並且能夠提供可能的修復程序的代碼段? – BruceyBandit

+0

如果您沒有創建視圖的權限,請嘗試是否可以創建臨時表(#)。將第一個選擇從組中選擇插入到臨時表和總覽中,如果有您的預期結果 - 如果沒有問題,請獲取第二個選擇並嘗試總結每個部分的臨時結果。 – Deadsheep39