[英]Inner join with 4 tables creating duplication
SELECT DISTINCT Analysed.resultId,bugOwner,Analysed.bugId as BugDet,bugType,testCaseName
FROM Bug
INNER JOIN Analysed
ON Analysed.bugId=Bug.bugId
INNER JOIN Results
ON Analysed.runId=Results.runId
WHERE Analysed.runId=64
以上工作正常。
现在我有另一个表结果(resultId,runId,analyze,testname)
我还想在我的其他查询中包含testname,所以我补充说,
SELECT Analysed.resultId,bugOwner,Analysed.bugId AS BugDet,bugType,testCaseName
FROM Bug
INNER JOIN Analysed
ON Analysed.bugId=Bug.bugId
INNER JOIN Results
ON Analysed.runId=Results.runId
WHERE Analysed.runId=64
但是这个查询重复了记录。我猜它采取了一些交叉产品或其他东西。 有谁知道如何解决它?
我假设问题是你在结果中有多行具有相同的runId。 如果没有办法将它们限制为仅限于第一个记录(例如,对于第一个记录总是具有runId 0),则可以获取具有外部apply的testCaseName的第一个:
SELECT
A.resultId,
B.bugOwner,
A.bugId AS BugDet,
B.bugType,
R.testCaseName
FROM
Bug B
INNER JOIN Analysed A ON A.bugId=B.bugId
outer apply (
select top 1
testCaseName
from
Results R
where
A.runId=R.runId and
R.testCaseName is not NULL) R
WHERE
A.runId=64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.