I have requirement to exclude first query data from 2nd query data i want to separate second query data what is the best way for that how can we you exclude first query EXCEPT and UNION syntax for that, When i adding UNION ALL all first query all data include second query data.Can you please advise on this.
Sample Query-
Alter FUNCTION Report_DailyStatusReport --trainnig(
@Consolidated VARCHAR(1),
@IncludeArchivedVoyages CHAR(1),
@CurrentCountry varchar(2),
@CompanyPK uniqueidentifier
)
RETURNS TABLE
AS
RETURN
--1st query
SELECT
distinct JS_UniqueConsignRef AS Booking_ID,
JS_Direction As Direction,
odrref.OdrNum AS orderno
from JobShipment
LEft Join JobPackLines on JS_PK=JL_JS
Left join JobcontainerPackPivot on J6_JL=JL_PK
WHERE JS_IsBooking = 1
AND JS_IsCancelled = 0
AND JS_IsForwardRegistered = 0 and JH_GC = @CompanyPK
AND (@IncludeArchivedVoyages = 'Y' OR JV_IsActive IS NULL OR JV_IsActive = 1)
Union ALL
--EXCEPT
--Second Query
SELECT
distinct JS_UniqueConsignRef AS Booking_ID,
JS_Direction As Direction,
odrref.OdrNum AS orderno
from JobShipment
LEft Join JobPackLines on JS_PK=JL_JS
Left join JobcontainerPackPivot on J6_JL=JL_PK
WHERE JS_IsBooking = 1 and JH_GC = @CompanyPK
AND JS_IsCancelled = 0
AND JS_IsForwardRegistered = 1
AND @Consolidated ='Y'