This is a real life example, please take a look at my sqlfiddle,
http://sqlfiddle.com/#!3/d8902/1
I have 3 tables, TickData where all data should go to and 2 source tables with data that have some overlapping data, [ES 09-11_Part01] and [ES 09-11_Part02].
Please note, although in the sqlfiddle I've only populated the 2 tables, ES 09-11_Part0X with a very small amount of data, I am in fact dealing with over 4 million rows each source table.
I need to Insert rows into TickData from the 2 tables and need to avoid duplicates, however 2 caveats.
Firstly, TickData.LastDateTime is a composite of [ES 09-11_Part0X].LastDate and [ES 09-11_Part0X].LastTime.
Secondly, duplicates are identified only if from [ES 09-11_Part0X], CQGTickId and LastDate with LastTime are equal to TickId and TickData.LastDateTime respectively, CQGTickId or TickId by itself is not unique, it must be combined with the date field.
How would I avoid inserting any duplicates into TickData, if the record already exists in TickData, do not insert into TickData from source table and if there is duplicate data in the source table, [ES 09-11_Part0X] it should be inserted one time only into TickData.
In my sqlfiddle, note that [ES 09-11_Part02] has 1 record which already exists in [ES 09-11_Part01]
('2011-04-05','14:12:07.4758734',12267,1,70,12267,12269,163377)
also [ES 09-11_Part02] contains another duplicate within its own table that occurs 3 times
('2011-04-06','00:47:31.2748398',12272,1,12,12269,12272,194813)
Therefore the final output of TickData after the 2 inserts from the source table should not include rows 6, 9 and 10. Only 1 occurrence of the data should exist.
This is a real life example for Stock Ticks data so it needs to be accurate and reliable please. I will also have to continually update TickData with additional source tables which may have again duplicate and overlapping values within.
Thanks in advance to any TSQL geniuses. :D
(original link:http://sqlfiddle.com/#!3/c504b/1)