2016-06-28 50 views
1

希望你們能幫助我。我是一名實習分析師,所以需要幫助。請看下面的查詢:排除別名欄中的'Null'

SELECT [PATHWAY_ID], [PAS_ID], [Patient_Name], [Pathway_Specialty], [Clinician], 
     [Referral_priority], [RTT_START_DTTM], [Days Waiting], [Activity Type], 
     [Last Activity Type], [Next_Appt_DTTM], [First_Appointment_Flag], 
     CASE WHEN [Days Waiting] <=97 AND [Next_Appt_DTTM] IS NOT NULL THEN 'Booked' 
      WHEN [Days Waiting] >=98 AND [Next_Appt_DTTM] IS NULL THEN 'Unbooked' 
     END AS 'Booked Status' 
FROM [GWH_RTT].[rtt].[GWH_RTT_Nonadmitted_PTL] 
WHERE [Referral_priority] = 'Routine' 
ORDER BY 'Booked Status' DESC 

在我的別名列中,我有空的那裏,不匹配我的case語句。我如何讓SQL返回我的case語句,排除我的別名列中的空值?

+0

這是一個CASE表達式(它返回一個值)。 case語句只是條件執行的代碼(例如在存儲過程中)。 – jarlh

+0

謝謝,沒有意識到:) – Ang

回答

1

如何使用子查詢或CTE:

with q as (<your query here without the order by>) 
select q.* 
from q 
where [Booked Status] is not null 
order by [Booked Status]; 

一句忠告:不要使用列別名單引號。僅對日期和字符串常量使用單引號。您可以在SQL Server中使用方括號或雙引號。

1

試試這個:

SELECT t.* 
FROM (SELECT [PATHWAY_ID], [PAS_ID], [Patient_Name], [Pathway_Specialty], [Clinician], 
     [Referral_priority], [RTT_START_DTTM], [Days Waiting], [Activity Type], 
     [Last Activity Type], [Next_Appt_DTTM], [First_Appointment_Flag], 
     CASE WHEN [Days Waiting] <=97 AND [Next_Appt_DTTM] IS NOT NULL THEN 'Booked' 
      WHEN [Days Waiting] >=98 AND [Next_Appt_DTTM] IS NULL THEN 'Unbooked' 
     END AS 'Booked Status' 
FROM [GWH_RTT].[rtt].[GWH_RTT_Nonadmitted_PTL] 
WHERE [Referral_priority] = 'Routine' 
) t 
WHERE t.[Booked Status] IS NOT NULL 
ORDER BY [Booked Status] DESC