Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:34:07+00:00 2026-06-10T04:34:07+00:00

What I am trying to accomplish Select up to two records from table Visit

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:34:09+00:00Added an answer on June 10, 2026 at 4:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT DISTINCT field1, field2, field3, ...... FROM table; I am trying to accomplish the
So I am trying to accomplish something like this: SELECT * FROM table WHERE
I am trying to accomplish the following: SELECT col1, col2 FROM table1 UNION SELECT
I'm trying to SELECT from two tables and ORDER BY date (a column they
I'm trying to accomplish one simple task (but is not simple for me). I
Which I'm trying to accomplish is have two classes, a non generic and a
Here's what I'm trying to accomplish: I've got two tables, call them first and
I am trying to get a single property from a joined table where a
This is what i'm trying to accomplish. I want to select only distinct values
I am trying to query an XML document for the specific records that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.