I have a simple select statement for SQL server, with a couple of joins. It just returns a list of products (by category).
What I need to do is have the very first product result be always the same (for just one of the categories). I don’t know how to accomplish that. Any advice? TYIA!
ORDER
BY CASE WHEN productname = 'this one'
THEN 'humpty' ELSE 'dumpty' END DESC
, category
, productname
for the desired product, the sort key is ‘humpty’ and for all others it’s ‘dumpty’, and since the sort is DESC, all humpties (there being only this one) come before all dumpties, and the dumpties are then sorted by category and product as before
Thanks for that. Problem is, I need to keep the other sort order in tact. My default sort order is: ORDER BY Rank, p.ID Desc. Rank is a list we keep of top-selling products, but not all products have a unique Rank. What I currently have for a data set is:
ID ProdName ProdCategory ProdPrice Rank etc
540 GreatProduct SameCat 15.00 1 etc
201 CoolWidget1 SameCat 21.50 2 etc
222 FavoriteOne SameCat 9.00 3 etc <---- this one should be first in the set
409 AnotherProd SameCat 35.50 4 etc
177 BadSeller3… SameCat 7.00 99 etc
421 WorstSelling SameCat 15.00 99 etc
What I want is:
ID ProdName ProdCategory ProdPrice Rank etc
222 FavoriteOne SameCat 9.00 3 etc <---- here it’s first, but Rank is still kept for the rest of the products
540 GreatProduct SameCat 15.00 1 etc
201 CoolWidget1 SameCat 21.50 2 etc
409 AnotherProd SameCat 35.50 4 etc
177 BadSeller3… SameCat 7.00 99 etc
421 WorstSelling SameCat 15.00 99 etc
I was wondering about a UNION - but would that be overkill?