零件可以在服務條目中使用,如果使用了零件,則服務條目零件表將記錄該零件。SQL:如果另一個結果集記錄了該ID,如何從CTE中刪除記錄
在下面的部分從未在服務條目中使用,所以我顯示適用於具有默認配置的系統的適用部分。運行報告時,是servicedate是2014年10月1日之間 - 在服務條目用於2014年10月6日部分 -
Instrument Part Number Part Description Revision Number Date xNumber
ICQ IA EP5 152970 RSH-Load Platform-Arm (Spring), 100
ICQ IA EP6 152970 RSH-Load Platform-Arm (Spring), 100
ICQ IA EP7 152970 RSH-Load Platform-Arm (Spring), 100
在接下來的2014年10月1日2014年10月2日
對於系統6和7,因爲有一個服務日期/時間,所以我包含服務條目部分表中的修訂號,並且因爲在系統5上它沒有被使用,所以我顯示了部分表中的默認配置。Instrument Part Number Part Description Revision Number Date xNumber
ICQ IA EP5 152970 RSH-Load Platform-Arm (Spring), 100
ICQ IA EP6 152970 RSH-Load Platform-Arm (Spring), 101 10/05/2014
ICQ IA EP7 152970 RSH-Load Platform-Arm (Spring), 101 10/05/2014
現在,如果我跑與提供給查詢作爲2014年10月1日的日期範圍的報告 - 2014年10月14日,你可以看到系統6對10/05其他服務項目。由於系統5從來沒有服務條目,我仍然在部件表中顯示缺省修訂編號。
Instrument Part Number Part Description Revision Number Date xNumber
ICQ IA EP5 152970 RSH-Load Platform-Arm (Spring), 100
ICQ IA EP6 152970 RSH-Load Platform-Arm (Spring), 101 10/05/2014
ICQ IA EP6 152970 RSH-Load Platform-Arm (Spring), 101 10/10/2014
ICQ IA EP7 152970 RSH-Load Platform-Arm (Spring), 101 10/05/2014
因此,我寫了下面的CTE,它基本上給了我適用於系統的每個部件及其部件表的修訂號。
DECLARE @run_log TABLE(
[Instrument] nvarchar(max),
[SubSystem] NVARCHAR(max),
[AbbottPartNumber] NVARCHAR(max),
[RSLMSPartID] int,
[PartDescription] NVARCHAR(max),
[PartRevisionNumber] NVARCHAR(max),
--[ServiceEntryID] int,
[Date] datetime,
[TSBNumber] nvarchar (max),
[CRNumber] nvarchar(max)
)
insert @run_log
select DISTINCT
System.SystemFullName as Instrument,
Part.System as SubSystem,
Part.ID as PartID,
Part.PartDescription,
Part.RevisionNumber as PartRevisionNumber,
--NULL as ServiceEntryID,
NULL as Date,
NULL as TSBNumber,
NULL as CRNumber
from Part
inner join InstrumentType on Part.InstrumentTypeID = InstrumentType.ID
inner join SystemModule on SystemModule.InstrumentTypeID = Part.InstrumentTypeID
inner join System on System.ID = SystemModule.SystemID
WHERE System.PlatformID = 2 and Part.Active = 1 and InstrumentType.Active = 1
and Part.ID = 152970
and Part.ParentID > 0
;WITH RunLogs AS(
SELECT *
FROM @run_log r
)
如果在服務條目部分表中有記錄,則以下查詢返回適用於系統的部分。如果我將CTE的結果與這個結果集合在一起,我接近我期望的報告輸出,但是我想要做的是如果零件在下面的查詢中有記錄,我需要從CTE上面刪除該零件記錄
-- Parts Installed in Entries inlc Baseline and/or user selected
select
System.SystemFullName as Instrument,
Part.System as Subsystem,
Part.ID as PartID,
Part.PartDescription,
COALESCE(ServiceEntryPart.PartRevisionNumber,Part.RevisionNumber) AS PartRevisionNumber,
--ServiceEntryPart.ServiceEntryID,
ServiceEntry.ServiceDateTime as Date,
ServiceEntry.TSBNumber,
ServiceEntry.CRNumber
from Part
inner join ServiceEntryPart on ServiceEntryPart.PartID = Part.ID
inner join ServiceEntry on ServiceEntry.ID = ServiceEntryPart.ServiceEntryID
inner join systemmodule on ServiceEntryPart.SystemModuleID = SystemModule.ID
inner join System on System.ID = SystemModule.SystemID
cross apply
dbo.SplitStrings_Moden(ServiceEntryPart.ServiceTypeIDs, N',') M2
JOIN dbo.SplitStrings_Moden('1', N',') P ON (M2.Item = P.Item or '1'IS NULL)
WHERE System.PlatformID = 2 and Part.Active = 1
and Part.ParentID > 0 AND Part.ID = 152970
編輯
如果我做UNION ALL,熱膨脹係數和第二查詢之間我得到的結果如下,這是非常接近我所需要的,但行7號和9不應顯示因爲它們來自CTE並且我不需要它們,因爲這些部分用於服務入口服務條目部分中,如果它們在CTE中被引用,則在第二個查詢中返回的基本部分,在結果中,你需要不被遺忘。
Instrument SubSystem RSLMSPartID PartDescription PartRevisionNumber Date TSBNumber CRNumber
ICQ IA EP00 - Dallas RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP1 - Dallas RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP2 - Dallas RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP3 - Dallas RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP4 - Dallas RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP5 - Dallas RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP6 - Lake County RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP6 - Lake County RSH 152970 Arm (Spring), Tray/Carrier Detect NULL 2014-10-09 17:35:00.000 NULL NULL
ICQ IA EP7 - Wiesbaden RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL
ICQ IA EP7 - Wiesbaden RSH 152970 Arm (Spring), Tray/Carrier Detect NULL 2014-10-09 17:10:00.000 NULL NULL
ICQ IA EPTraining - Training RSH 152970 Arm (Spring), Tray/Carrier Detect NULL NULL NULL NULL