我有一个如下查询,想通过将isMatchingNameEmail传递给第二种情况来整理它,
我的最终目标是通过以下逻辑找到匹配的referenceID:
返回数据库的referenceID 如果名字+电子邮件地址上有任何匹配项,如果不匹配,则尝试找到与名字和手机号上匹配的内容,如果不是,则返回
只想看看是否有更好的方法可以做到。 谢谢大家!
select r.*,
L.U3L_ReferenceID ReferenceID,
case when L.U3L_ReferenceID is null then
(select L.U3L_ReferenceID from [u3_data].[data].[ListData_e0a27] [L]
where r.given_name = L.firstname and r.email = L.emailaddress)
end isMatchingNameEmail,
-- when record is not found and isMatchingNameEmail is null
case when L.U3L_ReferenceID is null
and (select top 1 L.U3L_ReferenceID from [u3_data].[data].[ListData_e0a27] [L]
where r.given_name = L.firstname and r.email = L.emailaddress) is null
then
(select L.U3L_ReferenceID from [u3_data].[data].[ListData_e0a27] [L]
where
r.given_name = L.firstname and
(replace(
replace(
replace(
replace(r.mobilephone
,' ','')
,'(','')
,')','')
,'+','') = L.mobilenumber or
replace(
replace(
replace(
replace(r.mobilephone
,' ','')
,'(','')
,')','')
,'+','') = stuff(L.mobilenumber,1,2,'0') or
replace(
replace(
replace(
replace(r.mobilephone
,' ','')
,'(','')
,')','')
,'+','') = L.mobilenumber))
end isMatchingNameMobile
from @Records r
left Join [u3_data].[data].[ListData_e0a27] [L] with(nolock) on
[r].Id = [L].userid
答案 0 :(得分:0)
使用CTE
; with
cte1 as
(
select col1, col2, <query expression> as col3 from yourtable
),
cte2 as
(
select col1, col2, col3, col3 * 4 as col4 from cte1
)
select col1, col2, col3, col4
from cte2
使用派生表
select col1, col2, col3, col4
from
(
select col1, col2, col3, col3 * 4 as col4
from
(
select col1, col3, <query expression> as col3 from yourtable
) as d
) as d
使用应用运算符
select t.col1, t.col2, a3.col3, a4.col4
from yourtable t
cross apply
(
select <query expression> as col3
) a1
cross apply
(
select col3 * 4 as col4
) a