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 7367987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:36:27+00:00 2026-05-29T03:36:27+00:00

I have a search query that is dynamically compiled using NHibernate’s criteria query mechanism.

  • 0

I have a search query that is dynamically compiled using NHibernate’s criteria query mechanism. The resulting SQL query might look like:

select 
    *
from
    sometable
where
(
    (
        firstname like 'chris%' or
        lastname like 'chris%'
    )
    and
    (
        firstname like 'vann%' or
        lastname like 'vann%'
    )    
)

The data in the table might look like:

FirstName         LastName
------------------------------
Chris             Smith
John              Vann
Chris             Vann

I’d like to order the results such that a row matching both sub-clauses in the where clause (i.e. firstname = Chris and lastname = Vann) is ranked higher than a row matching only one of the sub-clauses. Is this possible in standard SQL?

Edit: I greatly simplified the question to get down to the guts of the problem.

  • 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-05-29T03:36:28+00:00Added an answer on May 29, 2026 at 3:36 am

    This is only a point to start. You can create a calculate priority column and sort rows by this column. The column is a indicator for well match row. Here a sample code writed for you:

    create table #t (f varchar(10), l varchar(10) );
    
    insert into #t values ('aa','ee'),('aa','ii'),('oo','ee');
    
    select 
       *,
       case when f like 'aa%' then 1 else 0 end +
       case when l like 'aa%' then 1 else 0 end +
       case when f like 'ii%' then 1 else 0 end + 
       case when l like 'aa%' then 1 else 0 end 
       as priority
    from #t
    order by 
       priority desc
    

    Results:

    f  l  priority 
    -- -- -------- 
    aa ee 4        
    aa ii 4        
    oo ee 0 
    

    For your schema may be something like:

    select 
        *.
        case when firstname like 'chris%' and lastname like 'vann%' then 4 else 0 +
        case when firstname like 'chris%' and lastname not like 'vann%' then 3 else 0 +
        case when firstname not like 'chris%' and lastname like 'vann%' then 3 else 0 +
        ...
        as priority
    from
        sometable
    where
    (
        (
            firstname like 'chris%' or
            lastname like 'chris%'
        )
        and
        (
            firstname like 'vann%' or
            lastname like 'vann%'
        )    
    )
    order by priority desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to query the windows desktop search API using SQL. I have
I have this sql search query that works, but it only works if votes
I have a search query that I'm inheriting and attempting to optimize. I am
I have a problem with a search query I am using my data contains
I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have built a T-SQL query like this: DECLARE @search nvarchar(1000) = 'FORMSOF(INFLECTIONAL,hills) AND
I have an application where I create a big SQL query dynamically for SQL
I have search query that has many optional parameters and then search word field
Basically I have a search query, which works fine if I'm using it with
I have a search query that works great, but I need it to only

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.