我有两个表Part和Service Entry Part。第一个语句返回所有部分,第二个语句返回服务类型为13的部分。
我需要做的是,如果第二个选择返回一个部分,那么第二个选择中的记录应该被包含,并且应该排除第一个选择中的部分
DECLARE @run_log TABLE(
[Instrument] nvarchar(max),
[SubSystem] NVARCHAR(max),
[AbPartNumber] NVARCHAR(max),
[RSLMSPartID] int,
[PartDescriptionWithParent] NVARCHAR(max),
[PartRevisionNumber] NVARCHAR(max),
[ServiceEntryID] int,
[Date] datetime,
[TSBNumber] nvarchar (max),
[CRNumber] nvarchar(max)
)
insert @run_log
-- parts with default revision number
select
System.SystemFullName as Instrument,
Part.System as SubSystem,
Part.AbPartNo as AbPartNumber,
Part.ID as RSLMSPartID,
Part.PartDescriptionWithParent,
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 ((@PlatformID =0) OR (System.PlatformID = @PlatformID) OR (@PlatformID = 12 AND System.PlatformID <= 2))
AND (@SelectedSystemID is null OR System.ID IN(select * from dbo.SplitInts_RBAR_1(@SelectedSystemID, ',')))
AND (@SelectedAbIDs is null OR Part.ID IN (select * from dbo.SplitInts_RBAR_1(@SelectedAbIDs,',')))
AND (@SelectedSubSystems is null OR Part.System IN (select * from dbo.SplitStrings_Moden(@SelectedSubSystems,',')))
AND System.Active = 1 and Part.Active = 1
ORDER BY SubSystem,RSLMSPartID
;WITH RunLogs AS(
SELECT *
FROM @run_log r
)
select * from RunLogs
UNION
select System.SystemFullName as Instrument,
Part.System as Subsystem,
Part.AbbottPartNo as AbPartNumber,
Part.ID as RSLMSPartID,
Part.PartDescriptionWithParent,
ServiceEntryPart.PartRevisionNumber 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('13', N',') P ON (M2.Item = P.Item)
WHERE System.Active = 1 AND Part.Active = 1
AND (@SelectedSystemID is null OR System.ID IN(select * from dbo.SplitInts_RBAR_1(@SelectedSystemID, ',')))
AND ((@PlatformID =0) OR (System.PlatformID = @PlatformID) OR (@PlatformID = 12 AND System.PlatformID <= 2))
AND (ServiceEntry.ServiceDateTime between @StartDate and @EndDate)
AND (@SelectedAbIDs is null OR Part.ID in (select * from dbo.SplitInts_RBAR_1(@SelectedAbIDs,',')))
AND (@SelectedSubSystems is null OR Part.System IN (select * from dbo.SplitStrings_Moden(@SelectedSubSystems,',')))