【发布时间】:2012-10-29 15:06:53
【问题描述】:
我正在使用 SQL Server 2005,使用 Web Developer 2010 进行查询,并且 min 函数似乎返回了多个值(对于返回的每个 ID,请参见下文)。理想情况下,我希望它只为每个 ID 返回一个。
SELECT Production.WorksOrderOperations.WorksOrderNumber,
MIN(Production.WorksOrderOperations.OperationNumber) AS Expr1,
Production.Resources.ResourceCode,
Production.Resources.ResourceDescription,
Production.WorksOrderExcel_ExcelExport_View.PartNumber,
Production.WorksOrderOperations.PlannedQuantity,
Production.WorksOrderOperations.PlannedSetTime,
Production.WorksOrderOperations.PlannedRunTime
FROM Production.WorksOrderOperations
INNER JOIN Production.Resources
ON Production.WorksOrderOperations.ResourceID = Production.Resources.ResourceID
INNER JOIN Production.WorksOrderExcel_ExcelExport_View
ON Production.WorksOrderOperations.WorksOrderNumber = Production.WorksOrderExcel_ExcelExport_View.WorksOrderNumber
WHERE Production.WorksOrderOperations.WorksOrderNumber IN
( SELECT WorksOrderNumber
FROM Production.WorksOrderExcel_ExcelExport_View AS WorksOrderExcel_ExcelExport_View_1
WHERE (WorksOrderSuffixStatus = 'Proposed'))
AND Production.Resources.ResourceCode IN ('1303', '1604')
GROUP BY Production.WorksOrderOperations.WorksOrderNumber,
Production.Resources.ResourceCode,
Production.Resources.ResourceDescription,
Production.WorksOrderExcel_ExcelExport_View.PartNumber,
Production.WorksOrderOperations.PlannedQuantity,
Production.WorksOrderOperations.PlannedSetTime,
Production.WorksOrderOperations.PlannedRunTime
如果您能理解它,我将从多个表中选择某些列,其中 WorksOrderNumber 也包含在子查询中,以及许多其他条件。
结果集看起来有点像,已经模糊了不相关的数据。
http://i.stack.imgur.com/5UFIp.png(不允许我嵌入图片)。
突出显示的行不应该存在,我无法明确将它们过滤掉,因为此结果集将每天更新,并且很可能发生在不同的记录中。
我尝试将 OperationNumber 转换为许多其他数据类型,varchar 类型返回“100”而不是“30”。也试过搜索引擎,好像没有人遇到同样的问题。
我没有对表格进行结构化(它们被严重标准化),并且无法重新构建它们。
任何想法表示赞赏,非常感谢。
【问题讨论】:
-
有没有办法在 IN() 函数之前的 WHERE 子句中有多个列?像这样:WHERE (WorksOrderNumber, OperationNumber) IN (SELECT WorksOrderNumber, OperationNumber FROM table)
-
请注意标签是独立的。也就是说,结合
sql和server并不意味着您在谈论MS SQL Server。请务必小心选择标签。
标签: sql sql-server min