cmick 77911 (4/29/2016)
I would turn it into a column and order by it. Cleaner this way.
SELECT
(CASE WHEN AddDt > UpdateDt
THEN AddDt
ELSE ISNULL(UpdateDt, AddDt)
END) AS MaxDt,
Id, Name, OnHand, AddDt, UpdateDt
FROM @widgits
ORDER BY 1
Using ordinal position is a bad practice. Anytime, anyone can change the column order and the results would change without notification. To prevent this, use column alias.
SELECT
(CASE WHEN AddDt > UpdateDt
THEN AddDt
ELSE ISNULL(UpdateDt, AddDt)
END) AS MaxDt,
Id, Name, OnHand, AddDt, UpdateDt
FROM @widgits
ORDER BY MaxDt