2011-05-03 97 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东西哪列,所以它的抱怨。