Solved

sql select order by priority

Posted on 2011-03-15
3
1,363 Views
Last Modified: 2012-05-11
Hi I have a table

id | custname | catagory | keyword
1     jack         |  cat1       | bikes
2     bill           |  cat1       | cars
3     frank       |  cat2       | bikes
4     john        |  cat1       | cars
5     joe          |  cat1       | cars

I need to select the top 4 based on priority of keyword.
If keyword has bikes then select (cat does not matter)
If there are 4 with the keyword bikes that is ok
If there are only 2 then the rest should be added by cat

My result should be
id | custname | catagory | keyword
1     jack         |  cat1       | bikes
3     frank       |  cat2       | bikes
2     bill           |  cat1       | cars
4     john        |  cat1       | cars

My statement look something like this: but does not order properly

SELECT TOP 4 id,custname,catagory,keyword
WHERE (keyword = 'bikes' or catagory='cat1')
 ORDER BY keyword, catagory, id,custname


Thanks In Advance
0
Comment
Question by:doctor069
[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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
Alex Matzinger earned 500 total points
ID: 35142707
Select TOP 4 id,custname,catagory,keyword 
FROM 
  (Select * 
  From table
  Where keyword = 'bikes'
  ORDER BY keyword, catagory, id,custname

  Union

  Select *
  From Table
  Where keyword != 'bikes'
 ORDER BY keyword, catagory, id,custname) temp

Open in new window

0
 
LVL 4

Expert Comment

by:Alex Matzinger
ID: 35142717
That will select all of the rows with bike and order them, and then put the rest of the rows underneath them, and then select the top 4 rows from that unioned table.  It should be what you need.
0
 

Author Closing Comment

by:doctor069
ID: 35142810
That did it! Thanks for your help
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

695 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