如何重新排序SQL查询?

时间:2012-04-20 14:30:56

标签: tsql parameters sql-server-2008

我正在为SSRS中的SSMS 2008 R2报告参数开发一个sproc。我目前正在使用此脚本填充此参数:

Select
    Null As [program_id],
    '-All-' As [program_name]
UNION ALL
SELECT DISTINCT
    [program_info_id] AS [program_id],
    [program_name]
FROM [evolv_reports].[dbo].[programs_view]
Order By [program_name]

但问题是现在有一个名为“(MS)Family Preservation Services”的新program_name。此程序列在“-All-”之前,因此现在此报告参数不再默认为“-All-”。如何让“-All-”再次上市?这是我当前参数输出的一部分:

program_id  program_name
406B3C38-D8D3-4ED5-AC5B-9013D3662931    (MS) Family Preservation Services
NULL                                    -All-
F6E2C172-2BFB-45BC-B870-E7BC2414DD91    Adoptions - Agency
C766E084-AB03-41AD-92B9-494FF3975E10    Adoptions - Subcontract
C21DACE0-8F34-433D-BE24-A79DE322F7EF    AL Step down foster care

2 个答案:

答案 0 :(得分:2)

SELECT [program_id], [program_name] FROM 
(
   Select 
  Null As [program_id], 
  '-All-' As [program_name] ,
  0 as num1
  UNION ALL 
 SELECT DISTINCT 
  [program_info_id] AS [program_id], 
  [program_name] ,
  1 as num1
)a
ORDER BY a.num1, a.[program_name] -- or just by a.num1 if you don't need to sort by program_name

答案 1 :(得分:0)

也许是这样的:

SELECT
    *
FROM
(
Select
    Null As [program_id],
    '-All-' As [program_name]
UNION ALL
SELECT DISTINCT
    [program_info_id] AS [program_id],
    [program_name]
FROM [evolv_reports].[dbo].[programs_view]
) AS T
Order By (CASE WHEN [program_id] IS NULL THEN 0 ELSE 1 END), [program_id]