当我加入一个参数时,我会得到结果:
join
SalesMapping m
on
(c.[MLIS Practice ID] = m.MLIS)
然而,当我加入2个参数时,我得到0个结果:
join
SalesMapping m
on
(c.[MLIS Practice ID] = m.MLIS and ltrim(rtrim(c.[QuikLab Practice Code]))=ltrim(rtrim(m.[Practice Code])))
我检查了我的源表,检查这个连接是否会产生任何结果,而且肯定会!
这是我的整个查询:
select
[rep name]=m.[Sales Rep],
[css name]=m.[CSS Rep],
m.[Practice Name],
m.[Practice Code],
[MLIS Code]=m.[MLIS],
[Yesterday Volume]=(select
COUNT([accession id])
from SpecimenCount
where [MLIS Practice ID]=m.[MLIS]
and [Date Received]=CAST(getdate()-2 as DATE)),
[Today Volume] = (select
COUNT([accession id])
from SpecimenCount
where [MLIS Practice ID]=m.[MLIS]
and [Date Received]=CAST(getdate()-1 as DATE)),
[Rep Email]=m.[sales rep],
[CSS Email]=m.[css rep],
[Manager Email]=m.manager
from
SpecimenCount c
join
SalesMapping m
on
(c.[MLIS Practice ID] = m.MLIS and c.[quiklab practice code]=m.[practice code])
join
ManagerNames
on ManagerNames.Name = m.Manager
为什么我的查询(加入两个条件)返回0结果?
这是我对以下内容的思考过程:
c.[MLIS Practice ID] = m.MLIS and c.[quiklab practice code]=m.[practice code]
我们绝对可以加入这两个数据集,原因如下:
+----------+----------+---------------+---------------+-----------------------+-----------+------------------+------------------+--------------+-----------+-----------+---------------+
| rep name | css name | Practice Name | Practice Code | quiklab practice code | MLIS Code | MLIS Practice ID | Yesterday Volume | Today Volume | Rep Email | CSS Email | Manager Email |
+----------+----------+---------------+---------------+-----------------------+-----------+------------------+------------------+--------------+-----------+-----------+---------------+
| na | na | na | SCPMA | SCPMA | 100004 | 100004 | 10 | 3 | na | na | na |
+----------+----------+---------------+---------------+-----------------------+-----------+------------------+------------------+--------------+-----------+-----------+---------------+
你可以看到代码100004
和SCPMA
是相同的,应该一起加入
答案 0 :(得分:2)
所以你已经验证了现有的MLIS和练习代码会加入......但是当你加入管理器时,确实存在第三个条件。我没有在您的示例数据中看到经理,但我愿意打赌,没有经理可以匹配mlis /练习代码对。
通常,识别这种情况的简单方法是逐个浏览您的联接,并注释掉或将其转换为左联接。一旦数据恢复,您就会知道导致问题的原因。
答案 1 :(得分:1)
您是否可以确定结果集中是否存在一行?你能验证表中的数据吗?我遇到此问题最常见的方法是使用错误的列名称。
哦,不要做部分别名,这给我带来了很多麻烦,最终也会给你带来麻烦。 (Alias ManagerNames)首先,验证MangerName.Name=SalesMapping.Manager
select distinct Name from ManagerNames
select distinct Name from ManagerNames
except
select distinct Manager from SalesMapping
--get less rows with the second query.
其次,验证SpecimenCount
之间是否存在匹配select distinct [MLIS Practice ID],[quiklab practice code] from SpecimenCount
select distinct [MLIS Practice ID],[quiklab practice code] from SpecimenCount
except
select MLIS,[practice code] from SalesMapping.
--get less rows with the second query.
您可能会发现,数据并不匹配。然后,您将不得不返回开发人员并获取有关如何填充表的更多信息,以便您可以匹配列。由于没有人费心使列名相同,我猜测匹配不会直截了当。
祝你好运。
答案 2 :(得分:1)
好吧,你可能没有空格,但有隐藏的,不可打印的字符(如TAB)?那个woudl会导致联接失败。