Solved

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

Posted on 2010-11-11
8
566 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

820 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