[英]How can I use a created column as a filter
我正在嘗試解決我在 SQL Server 中編寫的這個查詢的方法。
我需要能夠在DateDiff
函數中使用CASE
語句的結果。
如果相遇結束日期為空,那么我想使用相遇開始日期進行日期差異計算。
如果遇到結束日期不為空,那么我想使用遇到結束日期進行日期差異計算。
但是,當我將dateofservice
放入datediff
函數時,我不斷收到“無效的列名”錯誤。
這是我的代碼:
Select
e.encounterid,
Case
when e.encounterenddate = null
then e.encounterbegindate
else e.encounterenddate
end as dateofservice,
rf.submissiondate,
datediff(day, dateofservice, rf.submissiondate) as lagtime,
rf.healthplancode,
rf.transactiontypeid
from
udt_encounter as E
join
udt_receivedfile as RF on e.receivedfileid = rf.receivedfileid
where
rf.healthplancode = '00'
and rf.transactiontypeid = 1
and rf.submissiondate between '2020-01-01' and '2020-06-30'
order by
encounter.id asc
你甚至不需要CASE
, COALESCE
會做。
由於這很短,我會堅持簡單地重復表達式:
SELECT
e.encounterid,
COALESCE(e.encounterenddate, e.encounterbegindate) AS dateofservice
,rf.submissiondate
,datediff(day, COALESCE(e.encounterenddate, e.encounterbegindate), rf.submissiondate) AS lagtime
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.