Question

I have a relational database that I want to compare the differences of the data from two entries. Each entry may vary only slightly, but there may be around 100,000 rows to between the two entries to compare.

I put all of the data that I want to compare from both database entries into two separate temporary tables, which excludes all of the primary and foreign keys that are set up as unique values (so, they would always be different... and return a result).

The way I do it now is:

    Select PrimaryStream, SecondaryStream, FirstFrame
    From #tempTable1
    UNION
    Select PrimaryStream, SecondaryStream, FirstFrame
    From #tempTable2
    EXCEPT
    Select PrimaryStream, SecondaryStream, FirstFrame
    From #tempTable1
    INTERSECT
    Select PrimaryStream, SecondaryStream, FirstFrame
    From #tempTable2

I get the results of the differences that I want, but I also want to add the "PrimaryStreamName" to these results, which is in #tempTable1 and #tempTable2, but I can't use them for comparison because they will always be different.

Each PrimaryStreamName will be the same in #tempTable1 and each PrimaryStreamName will be the same in #tempTable2, but #tempTable1.PrimaryStreamName != #tempTable2.PrimaryStreamName.

So, my goal is to add the PrimaryStreamName to the result.

Thanks for your help!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top