我编写了一个 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')