?
Solved

SQL Order by 'starts with' then by 'contains'

Posted on 2010-11-11
8
Medium Priority
?
575 Views
Last Modified: 2012-05-10
I have an autocompleter for an ecommerce site where customers can enter text (@usrstr) and the query will return either products that start with that text or product categories that contain that text. The results are sorted alphabetically.  I want the results to sort first by records that start with the user's text then by records that contain that text.
If a customer enters in 'M', then results might look like
Combustion Motor
Damper
M2942-110
M2942-111
Motors
Sump Pumps

Currently Returns:
Combustion Motor
Damper
M2942-110
M2942-111
Motors
Sump Pumps

--Desired Result
M2942-110
M2942-111
Motors
Combustion Motor
Damper
Sump Pumps

-- Current Query
(SELECT TOP 5 itm_num
  FROM item
WHERE itm_num like @usrstr + '%')
union
(SELECT DISTINCT TOP 5 ctg_name
  FROM category
WHERE ctg_name like '%' + @usrstr + '%')

--Attempt at Desired Result Set
(SELECT TOP 5 itm_num
  FROM item
WHERE itm_num like @usrstr + '%')
union
(SELECT DISTINCT TOP 5 ctg_name
  FROM category
WHERE ctg_name like @usrstr + '%')
union
(SELECT DISTINCT TOP 5 ctg_name
  FROM category
WHERE ctg_name like '%' + @usrstr + '%')


In the end the query can only return one column of data.  Any help is appreciated.
0
Comment
Question by:mjmetzger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 2000 total points
ID: 34112975
You could try the following..

SELECT SearchTitle FROM
(SELECT TOP 5 itm_num as 'SearchTitle'
  FROM item
WHERE itm_num like @usrstr + '%'
union
SELECT DISTINCT TOP 5 ctg_name as 'SearchTitle'
  FROM category
WHERE ctg_name like '%' + @usrstr + '%')A
ORDER BY CHARINDEX(@usrstr,SearchTitle)
0
 
LVL 18

Expert Comment

by:deighton
ID: 34113000
use a case statement to calculate a field as 0 for first character is 'M' and 1 otherwise

then select all fields containing M and order by your calculated field.

here is an example on one of my tables, here using surname sort as an illustration

note quotes around calculated field 'init' in the sort - I find you need those

SELECT [EmployeeId],    
      [Surname]
      ,CASE SUBSTRING(SURNAME,1,1) WHEN 'M' THEN  0 ELSE 1 END as init
  FROM MyMaster
  WHERE SURNAME LIKE '%M%'
  ORDER BY 'init', surname
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34113013
This will also order based upon how close to the begining of the string the original search is. For example if the user searched for M they would get

MXXXXX
XMXXXX
XXMXXX
XXXMXX
XXXXMX
XXXXXM

You you want to further order these results with the matching position you could also do ORDER BY CHARINDEX(@usrstr,SearchTitle),SearchTitle
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Closing Comment

by:mjmetzger
ID: 34113122
Creative Solution!  
0
 

Author Comment

by:mjmetzger
ID: 34113170
EvilPostIt, what is the purpose of that 'A' after the parenthesis? I thought it was a typo at first, but the query won't run without it.  Aparently the character itself is arbitrary, but i've never seen it before.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34113183
You have to alias the table when using subqueries. If you were to do a join there would be no way to refer to that table as it does not have a name.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34113198
You can call it anything you like. I just chose A cause i was being last. You could call it BLAHBLAH if you really liked.
0
 

Author Comment

by:mjmetzger
ID: 34113373
perfect. thanks again!
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question