user3308219
user3308219

Reputation: 157

SQL - Order by column with specific value in front

I have the following query.

SELECT 
    A.*, B.QuantityOnHand, C.Brand, C.Category, C.Subcategory
FROM 
    item_info A
JOIN 
    inventory_status B ON A.ListID = B.ListID
LEFT JOIN 
    item_group C ON A.Parent_ListID = C.ListID
WHERE 
    (Conditions)
ORDER BY 
    Brand, Category, Subcategory, Name ASC

Let's assume I have brands A, B, G, X, and Z. I want to order the results so that brand X is first, followed by the other brands in ascending order. I also want to retain the ordering caused by the other columns, those being category, subcategory, and name.

The finished result would be something like this.

X
A
B
G
Z

How can I accomplish this?

Upvotes: 0

Views: 509

Answers (2)

gattoun
gattoun

Reputation: 106

I think you'd have to manually add them with a case statement.

ORDER BY 
     CASE Day 
     WHEN 'X' THEN 1
     ELSE 2
     END

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 81960

Another option

ORDER BY NullIf(Brand,'X'), Category, Subcategory, Name ASC

Brand X will be first, then it would preserve the remaining order of Brand

Upvotes: 3

Related Questions