NOT NULL值之间的升序和降序

时间:2013-04-17 17:42:38

标签: sql sql-server sql-server-2008

使用此查询:

WITH CTE
AS
(
    SELECT 
        concat(e.FirstName, ' ', e.LastName) as Employee_Name, 
        g.GrantName, 
        g.Amount, 
        case 
            when g.Amount IS NULL THEN 0 
            ELSE 1 
        END AS IS_NULL_Amount
    from Employee e 
        LEFT OUTER join [Grant] g
            ON g.EmpID = e.EmpID
)

SELECT 
    CTE.Employee_Name, 
    CTE.GrantName, 
    CTE.Amount 
from CTE
ORDER BY CTE.IS_NULL_Amount desc

我得到了这些结果:

Employee_Name      GrantName                   Amount
---------------------------------------------------------
Barry Brown        K-Land fund trust           15750.00
Lee Osako          TALTA_Kishan International  18100.00
David Kennson      BIG 6's Foundation%         21000.00
Eric Bender        Just Mom                    9900.00
David Lonning      92 Purr_Scents %% team      4750.00
David Lonning      Robert@BigStarBank.com      18100.00
David Lonning      www.@-Last-U-Can-Help.com   25000.00
David Lonning      Big Giver Tom               95900.00
James Newton       Mega Mercy                  55000.00
Terry O'Haire      Ben@MoreTechnology.com      41000.00
Sally Smith        Thank you @.com             21500.00
Barbara O'Neil     NULL                        NULL
Phil Wilconkinski  NULL                        NULL
Janis Smith        NULL                        NULL
Alex Adams         NULL                        NULL
John Marshbank     NULL                        NULL
Lisa Kendall       NULL                        NULL

(17 row(s) affected)

如何仅在非空值之间对ASC和DESC进行排序?

2 个答案:

答案 0 :(得分:0)

对IS_NULL_Amount和Amount

进行排序
ORDER BY CTE.IS_NULL_Amount desc,Amount asc

答案 1 :(得分:0)

请尝试以下订单:

order by cte.Is_Null_Amount desc, EmployeeName

或者

order by cte.Is_Null_Amount desc, Amount desc