2011-05-03 96 views
0

這是我嘗試運行查詢:錯誤在查詢中使用WHERE IN

Select Status from [transaction] where TransactionID IN (select MAX(CAST(TransactionID AS VARCHAR(36))), sum(debit) 
FROM [transaction] 
WHERE dbo.getday(StartSaleTime) >= '5/1/2011' and dbo.getday(StartSaleTime) <= '5/3/2011' and Status > -1 And TransactionNo like 'EL%' And TransactionType = 4 
GROUP BY CustomerID, debit HAVING (COUNT(CustomerID) > 1)) 

它返回此錯誤:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. 

回答

3

它告訴你到底什麼是錯誤的錯誤信息。當使用in時,您只能在選擇列表中指定一列。

如果你改變你的查詢,這應該工作正常。

Select Status from [transaction] where TransactionID 

IN (select MAX(CAST(TransactionID AS VARCHAR(36))) as [TransactionID] 
FROM [transaction] 
WHERE dbo.getday(StartSaleTime) >= '5/1/2011' and dbo.getday(StartSaleTime) <= '5/3/2011' and Status > -1 And TransactionNo like 'EL%' And TransactionType = 4 
GROUP BY CustomerID, debit HAVING (COUNT(CustomerID) > 1)) 

使用EXISTSIN

只有當你可以指定多個列,但
3

您選擇兩件事情,並嘗試使用與( )。在嘗試執行someId In(Id列表)時,您應該只選擇ID。

2

您的子查詢必須只返回一個字段。現在你回到二,讓整個查詢看起來有點像:

SELECT ... WHERE TransactionID IN ((a,b), (c,d), etc...) 

SQL Server不知道使用的IN東西哪列,所以它的抱怨。