SQL MIN() returns multiple values?

混江龙づ霸主 提交于 2019-12-13 07:37:24

问题


I am using SQL server 2005, querying with Web Developer 2010, and the min function appears to be returning more than one value (for each ID returned, see below). Ideally I would like it to just return the one for each 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

If you can get your head around it, I am selecting certain columns from multiple tables where the WorksOrderNumber is also contained within a subquery, and numerous other conditions.

Result set looks a little like this, have blurred out irrelevant data.

http://i.stack.imgur.com/5UFIp.png (Wouldn't let me embed image).

The highlighted rows are NOT supposed to be there, I cannot explicitly filter them out, as this result set will be updated daily and it is likely to happen with a different record.

I have tried casting and converting the OperationNumber to numerous other data types, varchar type returns '100' instead of the '30'. Also tried searching search engines, no one seems to have the same problem.

I did not structure the tables (they're horribly normalised), and it is not possible to restructure them.

Any ideas appreciated, many thanks.


回答1:


The MIN function returns the minimum within the group. If you want the minimum for each ID you need to get group on just ID. I assume that by "ID" you are referring to Production.WorksOrderOperations.WorksOrderNumber.

You can add this as a "table" in your SQL:

(SELECT Production.WorksOrderOperations.WorksOrderNumber,
       MIN(Production.WorksOrderOperations.OperationNumber) 
  FROM Production.WorksOrderOperations
 GROUP BY  Production.WorksOrderOperations.WorksOrderNumber)


来源:https://stackoverflow.com/questions/13305648/sql-min-returns-multiple-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!