Question

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

Was it helpful?

Solution

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:

CREATE TABLE #t (ID int, TestField varchar(255))

INSERT INTO #t SELECT Id, Test1 FROM Visit WHERE customer = CustomerCode AND TIMESTAMPDIFF(SQL_TSI_MONTH, DATE_IN, CurrentDate) <= 24
INSERT INTO #t SELECT Id, Test2 FROM Visit WHERE customer = CustomerCode AND TIMESTAMPDIFF(SQL_TSI_MONTH, DATE_IN, CurrentDate) <= 24
INSERT INTO #t SELECT Id, Test3 FROM Visit WHERE customer = CustomerCode AND TIMESTAMPDIFF(SQL_TSI_MONTH, DATE_IN, CurrentDate) <= 24
... -- repeat for each Test field

SELECT TOP 2 * FROM Visit WHERE Id IN (
  SELECT a.Id FROM #t a
  LEFT JOIN #t b
    ON a.Id > b.Id
    AND a.TestField = b.TestField
  GROUP BY a.Id
  HAVING count(b.TestField) = 0
)
ORDER BY Id

DROP TABLE #t

Depending on the size of the table you may need to add an index to the temp table, or it will be unbearably slow:

CREATE INDEX some_unique_name_index ON #t (ID, TestField)

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.

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