我想从SQL查询创建一系列行,在某些情况下,表中可能不存在其中一个排序参数。这就是我到目前为止所拥有的:
SELECT h.DocumentNumber,
e0.ID as ID, e0.LastName+', '+e0.FirstName AS Employee,
e0.VpgFunction AS 'VPG Function',
e0.VpgPosition AS 'VPG Title',
e0.Grade AS Grade,
h.Period AS Period,
e0.Region AS Region,
e0.Country AS Country,
e0.Location AS Location,
e0.Division AS Division,
e0.Subdivision AS Subdivision,
e1.LastName+', '+e1.FirstName AS 'First Evaluator',
e2.LastName+', '+e2.FirstName AS 'Second Evaluator',
e3.LastName+', '+e3.FirstName AS 'Third Evaluator',
h1.Data AS 'Overall Performance Evaluation',
h.State AS State
FROM EefHeader h INNER JOIN Employees e0 ON h.Employee =
e0.Username INNER JOIN Employees e1 ON h.FirstEvaluator =
e1.Username LEFT JOIN Employees e2 ON h.SecondEvaluator =
e2.Username LEFT JOIN Employees e3 ON h.ThirdEvaluator =
e3.Username LEFT JOIN EefHistory h1 ON h.DocumentNumber =
h1.DocumentNumber WHERE h1.DataElementId = 'ManagerOverallEvaluationRbl'
ORDER BY h.DocumentNumber;
但是,这就是问题所在。对于h1(EefHistory)中的给定DocumentNumber,可能没有一行DataElementId等于' ManagerrOverallEvaluationRbl'一点都不在这种情况下,我仍然希望通过“整体绩效评估”返回该行。留空或NULL或其他东西。是否可以在查询中执行此操作? (我只是问,因为查询的结果后来被数据绑定到一个表。另外,可能有也可能没有第二个和第三个求值程序,这就是我在那里使用LEFT JOIN的原因。)
答案 0 :(得分:2)
您有两种选择。
or
以处理空值将其移动到on子句会强制引擎在连接之前执行限制(应用为过滤器)。这反过来允许左连接按需运行。当你说在or上包含空值时,你不知道NULL是否是连接的结果,或者dataElementID本身的值是否为null;这就是为什么它首选对ON条款应用限制。
WHERE (h1.DataElementId = 'ManagerOverallEvaluationRbl' OR h1.dataElementID is null)
或
SELECT h.DocumentNumber,
e0.ID as ID, e0.LastName+', '+e0.FirstName AS Employee,
e0.VpgFunction AS 'VPG Function',
e0.VpgPosition AS 'VPG Title',
e0.Grade AS Grade,
h.Period AS Period,
e0.Region AS Region,
e0.Country AS Country,
e0.Location AS Location,
e0.Division AS Division,
e0.Subdivision AS Subdivision,
e1.LastName+', '+e1.FirstName AS 'First Evaluator',
e2.LastName+', '+e2.FirstName AS 'Second Evaluator',
e3.LastName+', '+e3.FirstName AS 'Third Evaluator',
h1.Data AS 'Overall Performance Evaluation',
h.State AS State
FROM EefHeader h INNER JOIN Employees e0 ON h.Employee =
e0.Username INNER JOIN Employees e1 ON h.FirstEvaluator =
e1.Username LEFT JOIN Employees e2 ON h.SecondEvaluator =
e2.Username LEFT JOIN Employees e3 ON h.ThirdEvaluator =
e3.Username LEFT JOIN EefHistory h1 ON h.DocumentNumber = h1.DocumentNumber and
h1.DataElementId = 'ManagerOverallEvaluationRbl'
ORDER BY h.DocumentNumber;
要处理注释中的附加要求,只需生成一个数据集,该数据集包含每个文档的最大修订版号并加入该集合。根据需求结果,我们可能必须改变现有的左连接,以便在h1和h2之间作为整个子选择进行此连接。不确定dataElementID是应用于两个连接还是仅适用于基于
的连接这是一种方法:只需将其添加为另一个左连接....
LEFT JOIN (Select max(h1.revsionNumber), documentNumber
from EefHistory group by documentNumber) h2
on h2.documentNumber = h1.documentNumber
and h2.RevisionNumber = h1.RevisionNumber
或者可能是这样。
SELECT h.DocumentNumber,
e0.ID as ID, e0.LastName+', '+e0.FirstName AS Employee,
e0.VpgFunction AS 'VPG Function',
e0.VpgPosition AS 'VPG Title',
e0.Grade AS Grade,
h.Period AS Period,
e0.Region AS Region,
e0.Country AS Country,
e0.Location AS Location,
e0.Division AS Division,
e0.Subdivision AS Subdivision,
e1.LastName+', '+e1.FirstName AS 'First Evaluator',
e2.LastName+', '+e2.FirstName AS 'Second Evaluator',
e3.LastName+', '+e3.FirstName AS 'Third Evaluator',
h1.Data AS 'Overall Performance Evaluation',
h.State AS State
FROM EefHeader h
INNER JOIN Employees e0
ON h.Employee = e0.Username
INNER JOIN Employees e1
ON h.FirstEvaluator = e1.Username
LEFT JOIN Employees e2
ON h.SecondEvaluator = e2.Username
LEFT JOIN Employees e3
ON h.ThirdEvaluator = e3.Username
LEFT JOIN (Select * from EefHistory h1
INNER JOIN (SELECT max(revsionNumber) mrn, documentNumber
FROM EefHistory GROUP BY documentNumber) h2
on h2.documentNumber = h1.documentNumber
and h2.mrn = h1.RevisionNumber
and h1.DataElementId = 'ManagerOverallEvaluationRbl'
ON h.DocumentNumber = z.DocumentNumber
ORDER BY h.DocumentNumber;
这里的目的是获取一组eefHistory,其中包含所有列,但只包含dataElementID为' ManagerOverallEvaluationRbl'
的最大文档和修订号。