为代码的大片段道歉,但我需要显示它们以显示我编译的相关调查。
下面我有两个单独的查询,一个是从登台架构(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以确保正确的机组成员在飞行中。但是,结果不正确,因为它输出如下:
我们知道该航班上有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 :(得分:0)
为stagingflight创建列(您可能会使用apropriete索引)。请始终设置varchar的大小(例如varchar - &gt; varchar(30))。
alter table staging.sabreassignedcrew
add stagingflight as cast(airline + cast(flightnumber as varchar(5)) + suffix as varchar(25)) persisted
创建视图。
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
第二张表的第二个视图。