2016-11-04 44 views
1

我一直在研究这个查询将近两天。下面有一张工作人员桌子。每个地点都有多位员工根据列出的alpha来获取他们需要照顾的客户名单。然而,阿尔法不是标准的单字符或双字符,而是他们将单个直到三个字母放在桌子上。我试图根据客户姓氏在这些alpha范围内获得客户名单和他们的分配人员,我尝试了很多查询,并且他们都在某个级别上失败。基于多个字符alpha的SQL Server 2008名称列表

enter image description here

我用一对夫妇的热膨胀系数,以获得订单的一切,试图在马克斯

declare @maxChar varchar(3) = (select min(len(val)) from (
select staffStartChar val from StaffAlphaList where staffLocId = 1 
union 
select staffEndChar from StaffAlphaList where staffLocId = 1 
)temp) 

    select customerName 
    , upper(LTRIM(RTRIM(
       LTRIM(RTRIM(ISNULL(employee.f_name, ''))) + ' ' + 
       LTRIM(RTRIM(ISNULL(employee.m_name, ''))) + ' ' + 
       LTRIM(ISNULL(employee.l_name, '')) 
       ))) 
    from Customers with (nolock) 
    left outer join StaffAlphaList with (nolock) 
     on StaffAlphaList.staffLocId= Customers.LocId 
    left outer join employee with (nolock) 
     on employee.empl_no = StaffAlphaList.staffId 
    where lower(left(customerLastName, @maxChar)) between StaffAlphaList.staffStartChar and StaffAlphaList.staffEndChar 
+0

设置[坏习惯踢 - 把NOLOCK无处不在](http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/) - 它是*不推荐*到处使用 - 相当相反! –

回答

1

我觉得这你想要做什么:

select c.*, sal.* 
from customers c left join 
    staffalphalist sal 
    on sal.staffLocId = c.LocId and 
     left(c.customerLastName, len(sal.startchar)) >= sa.startchar and 
     left(c.customerLastName, len(sal.endchar)) <= sa.endChar; 

其实,我我不确定你想要什么输出,但是这列出了分配给每个客户的工作人员。

+0

非常感谢,现在正在完美地工作。我认为我正在反思这一点,主要关注阿尔法集,而不是整体。 – JOZO

相关问题