Link to home
Start Free TrialLog in
Avatar of doctor069
doctor069Flag for Canada

asked on

sql select order by priority

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
ASKER CERTIFIED SOLUTION
Avatar of Alex Matzinger
Alex Matzinger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of doctor069

ASKER

That did it! Thanks for your help