我正在使用SQL Server 2005查詢Web Developer 2010,並且min函數似乎返回多個值(對於返回的每個ID,請參見下文)。理想情況下,我希望它只爲每個ID返回一個。SQL MIN()返回多個值?
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) – Charlie
請注意,標籤是獨立的。也就是說,結合'sql'和'server'並不意味着你在談論MS SQL Server。請務必小心選擇標籤。 – Charles