2013-04-17 49 views
1

與此查詢之間的遞減順序:升序和NOT NULL值

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  [email protected]  18100.00 
David Lonning  [email protected] 25000.00 
David Lonning  Big Giver Tom    95900.00 
James Newton  Mega Mercy     55000.00 
Terry O'Haire  [email protected]  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?

+0

請澄清你的「之類的ASC和DESC之間只有不意味着什麼NULL值「。你想排序什麼專欄,金額?並保持空值最後? – Alkini

回答

0

排序在IS_NULL_Amount和金額

ORDER BY CTE.IS_NULL_Amount desc,Amount asc 
0

by子句試試這個順序:

order by cte.Is_Null_Amount desc, EmployeeName 

或者

order by cte.Is_Null_Amount desc, Amount desc