我遇到了一个非常奇怪的问题,即查询返回大多数行而不是所有行。如果我在where子句中添加一个添加条件,则会显示缺失的行。
以下是我的询问:
SELECT T1.Table1ID,
SUM(ISNULL([T4].Qty,0)) TotalQty
FROM dbo.Table1 (NOLOCK) T1
INNER JOIN [dbo].Table2 (NOLOCK) T2 ON T1.Table2ID = T2.Table2ID
INNER JOIN [dbo].Table3 (NOLOCK) T3 ON T2.DocTypeID = T3.DocTypeID
INNER JOIN dbo.Table4 (NOLOCK) T4 ON T4.Table1ID = T1.Table1ID
WHERE T3.is1 = 0
GROUP BY T1.Table1ID
ORDER BY T1.Table1ID
这将返回除T1.Table1ID = 185671之外的所有行。如果我将其添加为类似的过滤器:
SELECT T1.Table1ID,
SUM(ISNULL([T4].Qty,0)) TotalQty
FROM dbo.Table1 (NOLOCK) T1
INNER JOIN [dbo].Table2 (NOLOCK) T2 ON T1.Table2ID = T2.Table2ID
INNER JOIN [dbo].Table3 (NOLOCK) T3 ON T2.DocTypeID = T3.DocTypeID
INNER JOIN dbo.Table4 (NOLOCK) T4 ON T4.Table1ID = T1.Table1ID
WHERE T3.is1 = 0
AND T1.Table1ID = 185671
GROUP BY T1.Table1ID
ORDER BY T1.Table1ID
现在该行显示。有什么想法吗?
编辑:删除NOLOCK提示后的新查询。
SELECT T1.Table1ID,
SUM(ISNULL([T4].Qty,0)) TotalQty
FROM dbo.Table1 T1
INNER JOIN [dbo].Table2 T2 ON T1.Table2ID = T2.Table2ID
INNER JOIN [dbo].Table3 T3 ON T2.DocTypeID = T3.DocTypeID
INNER JOIN dbo.Table4 T4 ON T4.Table1ID = T1.Table1ID
WHERE T3.is1 = 0
AND T1.Table1ID = 185671
GROUP BY T1.Table1ID
ORDER BY T1.Table1ID