如何按排序值选择数据顺序并按名称排序NULL

时间:2019-08-14 18:09:36

标签: sql sql-server sql-order-by

我编写了以下SQL查询,以从@tmp表变量中选择数据。

SELECT @rowCount AS [row-count],
       t.[row-no] AS [row-no],
       t.[ServiceID] AS ServiceID,
       t.ServiceName AS ServiceName,
       t.[BranchServiceSortValue] AS SortValue,
       (CASE WHEN t.OptIn = 1 THEN 'Yes' ELSE 'No' END) AS OptIn
FROM   @tmp t
       INNER JOIN dbo.Category
            ON  Category.CategoryId = t.FkCategoryId
       INNER JOIN dbo.ServiceType
            ON  ServiceType.ServiceTypeId = t.FkServiceTypeId
WHERE  t.[row-no] >= @startRow
       AND t.[row-no] <= @endRow

       ORDER BY t.BranchServiceSortValue,t.serviceName

根据@tmp表中的数据,我上面的查询返回以下输出。

| row-count | row-no | ServiceID | ServiceName | SortValue | OptIn |
|-----------|--------|-----------|-------------|-----------|-------|
| 24        | 4      | 1088      | AAB         | NULL      | No    |
| 24        | 5      | 1089      | AAC         | NULL      | No    |
| 24        | 6      | 1090      | AAD         | NULL      | No    |

| 24        | 1      | 1093      | GDGD        | 0         | Yes   |
| 24        | 7      | 1091      | EETETE      | 1         | Yes   |
| 24        | 8      | 1092      | CSCDF       | 2         | Yes   |
| 24        | 3      | 1086      | CXCX        | 3         | Yes   |
| 24        | 9      | 16        | ASA         | 4         | Yes   |
| 24        | 2      | 1087      | BFB         | 5         | Yes   |
| 24        | 10     | 7         | Mortgage    | 6         | Yes   |
| 24        | 11     | 17        | DDWW        | 7         | Yes   |
| 24        | 12     | 11        | IL          | 8         | Yes   |
| 24        | 13     | 5         | SAA         | 9         | Yes   |
| 24        | 14     | 9         | CD          | 10        | Yes   |

您可以根据我上面的查询看到,数据行按SortValue排序,而当SortValue = NULL时,这3行按其ServiceName排序,

但是I need to display SortValue = NULL rows at the bottom of the other rows。这意味着我需要在SortValue非NULL数据之后显示Null行,并且 SortValue = NULL应该按以下顺序显示其服务名称

我的预期输出是:

| row-count | row-no | ServiceID | ServiceName | SortValue | OptIn |
|-----------|--------|-----------|-------------|-----------|-------|
| 14        | 1      | 1093      | GDGD        | 0         | Yes   |
| 14        | 7      | 1091      | EETETE      | 1         | Yes   |
| 14        | 8      | 1092      | CSCDF       | 2         | Yes   |
| 14        | 3      | 1086      | CXCX        | 3         | Yes   |
| 14        | 9      | 16        | ASA         | 4         | Yes   |
| 14        | 2      | 1087      | BFB         | 5         | Yes   |
| 14        | 10     | 7         | Mortgage    | 6         | Yes   |
| 14        | 11     | 17        | DDWW        | 7         | Yes   |
| 14        | 12     | 11        | IL          | 8         | Yes   |
| 14        | 13     | 5         | SAA         | 9         | Yes   |
| 14        | 14     | 9         | CD          | 10        | Yes   |

| 14        | 4      | 1088      | AAB         | NULL      | No    |
| 14        | 5      | 1089      | AAC         | NULL      | No    |
| 14        | 6      | 1090      | AAD         | NULL      | No    | 

我应该如何更改查询以获得高于输出的值?请帮助我

2 个答案:

答案 0 :(得分:2)

只需将密钥添加到ORDER BY

ORDER BY (CASE WHEN t.BranchServiceSortValue IS NOT NULL THEN 1 ELSE 2 END),
         t.BranchServiceSortValue, t.serviceName

SQL标准为NULLS FIRST子句提供了选项NULLS LASTORDER BY。 SQL Server尚未(尚未)实现这些。

答案 1 :(得分:2)

NULL的最小值最低,因此您需要使用CASENULL放在最后,然后按SortValue进行排序:

ORDER BY CASE WHEN t.BranchServiceSortValue IS NULL THEN 1 ELSE 0 END,
         t.BranchServiceSortValue,
         t.serviceName;