Dienstag, 16. Juni 2015

T-SQL: bring a dedicated value on top of a list

Problem: 

If you want to provide a selection list, i.e. for a report, you may want to have an 'All' entry always on top of the list.

Solution:

SELECT Name FROM (
    SELECT 'All'       as Name
    UNION
    SELECT DISTINCT    Item
    FROM               List
) T
ORDER BY CASE Name WHEN 'All' THEN 0 ELSE 1 END, Name

Keine Kommentare: