SQL查询返回值超出指定范围
SQL query returning values outside the specified range
当来自同一 table 的特定列有 4 个定义值时,我已经为 return 和 DocumentID
编写了一个 SQL 查询。我遇到的问题是我需要从另一列为每个值定义 ID,以确保我有一个准确的 return.
我写的查询是;
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')
你很接近:
(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。因此需要或。
当来自同一 table 的特定列有 4 个定义值时,我已经为 return 和 DocumentID
编写了一个 SQL 查询。我遇到的问题是我需要从另一列为每个值定义 ID,以确保我有一个准确的 return.
我写的查询是;
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')
你很接近:
(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。因此需要或。