1

使用此查询:

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 进行排序?

4

2 回答 2

0

排序 IS_NULL_Amount 和 Amount

ORDER BY CTE.IS_NULL_Amount desc,Amount asc
于 2013-04-17T17:47:08.983 回答
0

试试这个 order by 子句:

order by cte.Is_Null_Amount desc, EmployeeName

或者

order by cte.Is_Null_Amount desc, Amount desc
于 2013-04-17T17:47:37.337 回答