I am developing an SSRS 2008 report and am trying to get one of my stored procs to output “-All-” unioned with the other possible values for this parameter, where “-All-” appears as the first value. Instead, “-All-” is sorted in alphabetical order so that “Adams” is listed first. How do I make “-All-” come first? (Note that this parameter is a uniqueidentifier, so I cannot make “-All-” = -1. Here is my T-SQL code right now:
Select NULL As [client_id], NULL AS [id_no], '-All-' As [full_name], '-All-' As [id_and_name]
UNION ALL
Select Distinct [people_id] AS [client_id], [id_no], [full_name], [full_name] + ' : ' + [id_no] AS [id_and_name]
From [evolv_cs].[dbo].[service_track_current_view] With (NoLock)
Order By [full_name]
When I tried this code:
Select NULL As [client_id], NULL AS [id_no], '-All-' As [full_name], '-All-' As [id_and_name]
UNION ALL
Select Distinct [people_id] AS [client_id], [id_no], [full_name], [full_name] + ' : ' + [id_no] AS [id_and_name]
From [evolv_cs].[dbo].[service_track_current_view] With (NoLock)
Order By
CASE [full_name] WHEN '-All-' THEN 0 ELSE 1 END,
[full_name]
I got this error:
Msg 207, Level 16, State 1, Line 6
Invalid column name 'full_name'.
Msg 104, Level 16, State 1, Line 6
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
If it’s possible for you to add a column, then you could add
0 AS [sort_value]to the first part of the union and1 AS [sort_value]to the second part of the union.Then simply
Order By [sort_value],[full_name]