Please, check the performance, but following query should give expected results:
Select PrimaryStream, SecondaryStream, FirstFrame, '1' PrimaryStreamName
From #tempTable1
UNION ALL
Select PrimaryStream, SecondaryStream, FirstFrame, '2' PrimaryStreamName
From #tempTable2
EXCEPT
(
(
Select PrimaryStream, SecondaryStream, FirstFrame, '1' PrimaryStreamName
From #tempTable1
INTERSECT
Select PrimaryStream, SecondaryStream, FirstFrame, '1' PrimaryStreamName
From #tempTable2
)
UNION ALL
(
Select PrimaryStream, SecondaryStream, FirstFrame, '2' PrimaryStreamName
From #tempTable1
INTERSECT
Select PrimaryStream, SecondaryStream, FirstFrame, '2' PrimaryStreamName
From #tempTable2
)
)
Note, that there's a trick: I perform INTERSECT
two times (and I use UNION ALL
, since it's faster than UNION
), so the query operates on more rows during some operations.