3

我编写了一个 SQL 查询来返回一个DocumentID当来自同一个表的特定列有 4 个定义的值时。我遇到的问题是我需要为每个值定义另一列的 ID,以确保我有准确的返回。

我写的查询是;

SELECT DISTINCT 
    VV.[DocumentID], [Filename], [Path], D.LatestRevisionNo
FROM
    Documents D 
INNER JOIN
    [dbo].[DocumentsInProjects] DP ON DP.DocumentID = D.DocumentID 
INNER JOIN
    [dbo].[Projects] P ON DP.ProjectID = P.ProjectID 
INNER JOIN
    [dbo].[VariableValue] VV ON VV.DocumentID = D.DocumentID
WHERE 
    VV.ValueText IN ('170', '316', '2.125', 'LOCK RING') 
GROUP BY 
    VV.DocumentID, D.Filename, P.Path, D.LatestRevisionNo
HAVING 
    COUNT(DISTINCT ValueText) = 4 
    AND Filename LIKE '%sld%'

我需要的是类似

(VV.variableID = 79 AND VV.ValueText = 'LOCK RING') 
AND (VV.variableID = 92 AND VV.ValueText = '316') 
AND (VV.variableID = 328 AND VV.ValueText = '2.125') 
AND (VV.variableID = 351 AND VV.ValueText = '170')
4

1 回答 1

3

你很接近:

(VV.variableID = 79 AND VV.ValueText = 'LOCK RING') 
OR (VV.variableID = 92 AND VV.ValueText = '316') 
OR (VV.variableID = 328 AND VV.ValueText = '2.125') 
OR (VV.variableID = 351 AND VV.ValueText = '170')

variableId 不能是 79 和 92 以及 328 和 351 都在同一条记录上。因此需要或。

于 2015-11-23T21:09:35.990 回答