Solved

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

Posted on 2010-11-11
8
560 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
  • 4
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 500 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
 

Author Closing Comment

by:mjmetzger
ID: 34113122
Creative Solution!  
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now