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

  • SEARCH
  • Home
  • 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 7921743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:41:59+00:00 2026-06-03T16:41:59+00:00

I’m trying to do a query that can return me results where the most

  • 0

I’m trying to do a query that can return me results where the most of the 5 conditions match. But if there is a single 5 of 5 match, then that takes precedence.

To illustrate my question the followig SQL has been prepared.

declare @tmp table (
id int identity
,field1 nvarchar(60)
,field2 nvarchar(60)
,field3 nvarchar(60)
,field4 nvarchar(60)
,field5 nvarchar(60)
)

insert into @tmp values
    ('Bob','Jones','Mr','000001','bob@example.com')
insert into @tmp values
    ('Bill','Jones','','000002','bill@example.com')
insert into @tmp values
    ('Sue','Jones','Mrs','000003','jones@example.com')
insert into @tmp values
    ('Adrian','Jones','','000001','jones@example.com')
insert into @tmp values
    ('Bertha','Jones','Mrs','000001','jones@example.com')

select *
from @tmp

declare @key1 nvarchar(60), @key2 nvarchar(60), @key3 nvarchar(60), @key4 nvarchar(60), @key5 nvarchar(60)

select
    @key1 = 'Bertha'
    ,@key2 = 'Jones'
    ,@key3 = 'Mrs'
    ,@key4 = '000001'
    ,@key5 = 'jones@example.com'

select
    *
    ,case when field1 = @key1 then 1 else 0 end as X1
    ,case when field2 = @key2 then 1 else 0 end as X2
    ,case when field3 = @key3 then 1 else 0 end as X3
    ,case when field4 = @key4 then 1 else 0 end as X4
    ,case when field5 = @key5 then 1 else 0 end as X5
from @tmp

If you look at the results, you can see several Rows 3 and 4 matches on 3 fields, but Row 5 matches on 5 fields. Therefore this is an identical match and that’s the one I want returned.

But, if Row 5 wasn’t inserted, then Rows 3 and 4 are the best matches, in that case, I’d want those returned.

I’ve been trying to think how best to do this, I am using SQL Server 2008 if that can make any difference.

In the real scenario, they are not all simple case statements as in this example, but sub-selects into other tables.

I’ve looked into group by, and having, but I couldn’t see how I could use them in this scenario.

How can I do a ‘best of’ type match across multiple conditions like this in SQL Server?

If this appears ‘subjective’ as the page is telling me, say so and I’ll delete it. But I don’t think this is subjective as this is a SQL de-duplication scenario that I’d imagine is commonly requested.

  • 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-03T16:42:02+00:00Added an answer on June 3, 2026 at 4:42 pm

    Consider rolling your x columns into a SUM to get the score. Here’s a CTE (Common Table Expression) from which you can query.

    ;with mysum AS (
    select    *
        ,case when field1 = @key1 then 1 else 0 end as X1
        ,case when field2 = @key2 then 1 else 0 end as X2
        ,case when field3 = @key3 then 1 else 0 end as X3
        ,case when field4 = @key4 then 1 else 0 end as X4
        ,case when field5 = @key5 then 1 else 0 end as X5
    from @tmp)
    
    SELECT id, field1, field2, field3, field4, field5, 
           SUM(x5+x4+x3+x2+x1) AS MatchScore 
    FROM MySum
    GROUP BY id, field1, field2, field3, field4, field5
    ORDER BY MatchScore DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.