Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic SQL query Order By Value Question? 1

Status
Not open for further replies.

gus121

Technical User
May 9, 2002
298
GB
I have a basic query in Vbscript SQL:
"SELECT Link_ID, Link_Text FROM tblLinks WHERE Category_ID = "&gParentCategoryID&" ORDER BY Link_Text;"

I wish to change this query so instead of just retrieving records that meet a certain criteria i.e in this case Categoty_ID it retrieves all data from the table however i wish to display those records which meet the criteria of the particular Category_Id to be ordered at the top.

ie. query will look something like this

SELECT Link_ID, Link_Text FROM tblLinks ORDER BYCategory_ID = "&gParentCategoryID&" AND Link_Text;"

I know its an easy one but thanks for your help
do i have to have two select statments if so can i combine them?.

thanks for help to SQL newbie



-Gus
 
Code:
order by case when Category_ID = "&gParentCategoryID&" 
 then 0 else 1 end,link_text
 
Hi i tried compiling a slightly more advanced query using the order by case why does this not work?

SELECT Link_ID, Link_Text, Category_ID
FROM tblLinks
ORDER BY CASE WHEN Category_ID = 1 THEN 0 ELSE 1 END, CASE WHEN Category_ID =
(SELECT SubCategory_ID
FROM tblCategories
WHERE Category_ID = 1) THEN 0 ELSE 1 END, link_text; "

Any suggested methods of achiving this? thanks

-Gus
 
ops ignore this post the eproblem was the end speach quote left in by accident

-Gus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top