我有一些临时表(#c)和一些列。其中一列包含客户端的名称,我只需要进行搜索(没有id或其他有用的东西)。为了从客户端获取更多信息,我有一个包含人员数据的表格。
以下查询不完整,但主要想法是存在的。有什么问题?
select *
from #c c
left join (
select
case
when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 1 then 0
when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 2 then 1
when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) >= 3 then
select @varA1 = SUBSTRING(c.Client, 1, CHARINDEX(' ', c.Client + ' ')-1)
select @varN = SUBSTRING(c.Client, CHARINDEX(' ', c.Client + ' ')+1, LEN(c.Client))
select @varA2 = SUBSTRING(@varN, 1, CHARINDEX(' ', @varN + ' ')-1)
select @varN = SUBSTRING(@varN, CHARINDEX(' ', @varN + ' ')+1, LEN(@varN))
select top 1 UPPER(LTRIM(RTRIM(m.txt_a1)) + ' ' + LTRIM(RTRIM(m.txt_a2)) + ' ' + LTRIM(RTRIM(m.txt_name))) as FClient, m.txt_sex, tc.txt_desc as 'Status'
from person m
inner join test tc on m.cod_est = tc.cod_est
where UPPER(LTRIM(RTRIM(m.txt_a1))) = UPPER(LTRIM(RTRIM(@varA1))) and UPPER(LTRIM(RTRIM(m.txt_a2))) = UPPER(LTRIM(RTRIM(@varA2))) and UPPER(LTRIM(RTRIM(m.txt_name))) = UPPER(LTRIM(RTRIM(@varN)))
end
) x
on UPPER(LTRIM(RTRIM(c.Client))) like '%' + x.FClient + '%'
PD:由于我的人员表中有重复的记录,因此我列出了前1个条件。有时临时表中的记录不会存在于person表中。
答案 0 :(得分:0)
谢谢你,费拉斯,感谢您的支持。我看到并纠正了错误。我理解你的观点,Sean Lange,所有评论都非常有用。
我重新编写了如下查询,它正在运行。我认为这很慢。您是否建议使用索引或其他方法来改善时间?此致
select *
from #c c
left join (
select c1.[ID Incident], c1.[Paid to], c1.Client,
case
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 1 then ''
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 2 then ''
when LEN(UPPER(LTRIM(RTRIM(c1.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c1.Client))), ' ', '')) >= 3 then
(
select top 1 UPPER(LTRIM(RTRIM(m.txt_a1)) + ' ' + LTRIM(RTRIM(m.txt_a2)) + ' ' + LTRIM(RTRIM(m.txt_name))) as Client
from mpersona m
where UPPER(LTRIM(RTRIM(m.txt_a1))) = UPPER(LTRIM(RTRIM(SUBSTRING(c1.Client, 1, CHARINDEX(' ', c1.Client + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_a2))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), 1, CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_name))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')+1, LEN(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)))))))
)
end as 'Client',
case
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 1 then ''
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 2 then ''
when LEN(UPPER(LTRIM(RTRIM(c1.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c1.Client))), ' ', '')) >= 3 then
(
select top 1 m.date_birth
from mpersona m
where UPPER(LTRIM(RTRIM(m.txt_a1))) = UPPER(LTRIM(RTRIM(SUBSTRING(c1.Client, 1, CHARINDEX(' ', c1.Client + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_a2))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), 1, CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_name))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')+1, LEN(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)))))))
)
end as 'date_birth',
case
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 1 then ''
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 2 then ''
when LEN(UPPER(LTRIM(RTRIM(c1.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c1.Client))), ' ', '')) >= 3 then
(
select top 1 m.txt_sex
from mpersona m
where UPPER(LTRIM(RTRIM(m.txt_a1))) = UPPER(LTRIM(RTRIM(SUBSTRING(c1.Client, 1, CHARINDEX(' ', c1.Client + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_a2))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), 1, CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_name))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')+1, LEN(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)))))))
)
end as 'txt_sex',
case
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 1 then ''
--when LEN(UPPER(LTRIM(RTRIM(c.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c.Client))), ' ', '')) = 2 then ''
when LEN(UPPER(LTRIM(RTRIM(c1.Client))))-LEN(REPLACE(UPPER(LTRIM(RTRIM(c1.Client))), ' ', '')) >= 3 then
(
select top 1 tc.txt_desc as 'State'
from mpersona m
inner join test_civil tc on m.cod_est_civil = tc.cod_est_civil
where UPPER(LTRIM(RTRIM(m.txt_a1))) = UPPER(LTRIM(RTRIM(SUBSTRING(c1.Client, 1, CHARINDEX(' ', c1.Client + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_a2))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), 1, CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')-1)))) and UPPER(LTRIM(RTRIM(m.txt_name))) = UPPER(LTRIM(RTRIM(SUBSTRING(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)), CHARINDEX(' ', SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)) + ' ')+1, LEN(SUBSTRING(c1.Client, CHARINDEX(' ', c1.Client + ' ')+1, LEN(c1.Client)))))))
)
end as 'State'
from #c c1
) x
on c.[ID Incident] = x.[ID Incident] and c.[Paid to] = x.[Paid to] and c.Client = x.Client