What I am trying to accomplish
Select up to two records from table Visit that contain one of a number of codes in fields Test1-Test8 Within the last 2 years.
But the two records cannot have any duplicate codes.
ie Lets say Record1 contains ‘85.43’ in Test4
and Record2 contains ‘85.43’ in Test2
I would not want it to return Record2 because a Record with ‘85.43’ already exists.
Anyone know how I might accomplish this?
Here is my initial query that does not have the duplicate logic built into it.
select TOP 2 * from Visit where customer = CustomerCode AND
(Test1 IN ('85.41', '85.43', '85.45', '85.47')
or Test2 IN ('85.41', '85.43', '85.45', '105.47')
or Test3 IN ('85.41', '85.43', '85.45', '105.47')
or Test4 IN ('85.41', '85.43', '85.45', '105.47')
or Test5 IN ('85.41', '85.43', '85.45', '105.47')
or Test6 IN ('85.41', '85.43', '85.45', '105.47')
or Test7 IN ('85.41', '85.43', '85.45', '105.47')
or Test8 IN ('85.41', '85.43', '85.45', '105.47'))
AND TIMESTAMPDIFF(SQL_TSI_MONTH, DATE_IN, CurrentDate) <= 24;
Thanks
This is the cleanest way I can think of doing this, without resorting to all 64 comparisons that would be required if using the table directly:
Depending on the size of the table you may need to add an index to the temp table, or it will be unbearably slow:
Another alternative to speed this up would be to use a T-SQL loop to find one row at a time that matches the criteria and add them to a result table. Once you have enough results (2 in this case), you can exit the loop. For very large tables this would probably be the recommended approach.