SQL排序和添加行

时间:2016-08-19 20:41:12

标签: sql sql-server row distinct

这是我的SQL代码:

SELECT DISTINCT 
    CASE 
       WHEN RS != '' 
          THEN RS 
          ELSE 'Unspecified RS' 
    END AS 'RS Name'
FROM 
    OutstandingClaims

它会返回

Alexa
Bob
Cary
Dylan
...
Unspecified RS
...
etc.

我想要输入" All"在此列表的顶部。有没有一种方法可以在不重命名人的情况下做到这一点?

3 个答案:

答案 0 :(得分:1)

npm install -g typescript

答案 1 :(得分:0)

您可以尝试添加额外的列进行排序

SELECT 'All' AS [RS Name], 0 AS sort
UNION
SELECT CASE WHEN RS != '' 
         THEN RS 
         ELSE 'Unspecified RS' 
       END AS [RS Name],
       1 AS sort
FROM OutstandingClaims
ORDER BY sort, [RS Name]

请注意,UNION默认返回不同的记录。无需DISTINCT关键字。

答案 2 :(得分:0)

我不确定你究竟在问什么。但是如果您只需要在顶部显示“全部”字符串始终,那么您可以使用union和另一列来对它们进行排序。

所以,

Select x.rsname from( select 'All' AS rsname, 1 as order
  union
  select distinct (case when RS != '' then RS else 'Unspecified RS' end) AS rsname, 2 as order
 from 
OutstandingClaims) x order by x.order