2

我有三张桌子

表格1:

tblCustomer
CustomerID CustomerName
1  ABC Bank
2 Chase Bank

表 2:

tblOrderType
OrderTypeID OrderTypeName
1   Assignment
2   LienRelease

表3:

tblOrder
OrderID CustomerID  OrderTypeID LoanNumber
1   1   1   45584565
2   1   1   45566856
3   1   1   45565584
4   1   1   45588545

我正在尝试按顺序返回CustomerID,和CustomerName,但是在使用以下查询时:OrderTypeNameOrderCountCustomerName

SELECT tblOrder.CustomerID,CustomerName,OrderTypeName,COUNT(tblOrder.CustomerID)  FROM tblOrder
INNER JOIN tblCustomer ON tblOrder.CustomerID=tblCustomer.CustomerID
INNER JOIN tblOrderType ON tblOrder.OrderTypeID=tblOrderType.OrderTypeID
GROUP BY tblOrder.CustomerID

我收到错误消息:

Column 'tblCustomer.CustomerName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 

我不知道我做错了什么。

4

5 回答 5

2

您的SELECTGROUP BY语句应该具有相同的字段。在你的情况下

SELECT tblOrder.CustomerID,CustomerName,OrderTypeName

于 2015-04-27T13:07:13.200 回答
0

像这样的东西

SELECT tblOrder.CustomerID, CustomerName, OrderTypeName, COUNT(*)
FROM tblOrder
INNER JOIN tblCustomer ON tblOrder.CustomerID=tblCustomer.CustomerID
INNER JOIN tblOrderType ON tblOrder.OrderTypeID=tblOrderType.OrderTypeID
GROUP BY tblOrder.CustomerID, CustomerName, OrderTypeName

或这个

SELECT tblOrder.CustomerID, CustomerName, COUNT(*)  
FROM tblOrder
INNER JOIN tblCustomer ON tblOrder.CustomerID = tblCustomer.CustomerID
GROUP BY tblOrder.CustomerID, CustomerName
于 2015-04-27T13:08:29.730 回答
0

答案正是错误消息告诉您的内容。

您不能选择不在 group by 子句中的列或以某种方式聚合的列。

您也没有包含实际的 order by 子句。

SELECT 
    tblOrder.CustomerID,
    tblCustomer.CustomerName,
    tblOrderType.OrderTypeName,
    COUNT(tblOrder.CustomerID)  
FROM 
    tblOrder
    INNER JOIN tblCustomer ON tblOrder.CustomerID=tblCustomer.CustomerID
    INNER JOIN tblOrderType ON tblOrder.OrderTypeID=tblOrderType.OrderTypeID
GROUP BY 
    tblOrder.CustomerID,
    tblCustomer.CustomerName,
    tblOrderType.OrderTypeName,
ORDER BY
    tblCustomer .CustomerName ASC
于 2015-04-27T13:09:00.053 回答
0

您需要聚合GROUP BY语句中未包含的列:

SELECT A.CUSTOMERID, MAX(B.CUSTOMERNAME), MAX(C.ORDERTYPENAME), COUNT(A.CUSTOMERID)  
FROM TBLORDER AS A
INNER JOIN TBLCUSTOMER  AS B
ON A.CUSTOMERID=B.CUSTOMERID
INNER JOIN TBLORDERTYPE AS C
ON A.ORDERTYPEID=C.ORDERTYPEID
GROUP BY A.CUSTOMERID

或者,您可以按SELECT.

于 2015-04-27T13:14:17.980 回答
0

如果您GROUP BY在 SQl-Server 中使用,您必须聚合所有选定的列(fe 和Min,等) MaxCount或者您必须将它们包含在GROUP BY. 这是有道理的,因为尚不清楚您要为哪一行取值,CustomerName并且OrderTypeName每个 id-group 可能有多个值。

如果不是这种情况,请将其包含在Group by

SELECT tblOrder.CustomerID,
       CustomerName,
       OrderTypeName,
       COUNT(tblOrder.CustomerID) As CountCustomerID
FROM tblOrder
INNER JOIN tblCustomer ON tblOrder.CustomerID=tblCustomer.CustomerID
INNER JOIN tblOrderType ON tblOrder.OrderTypeID=tblOrderType.OrderTypeID
GROUP BY tblOrder.CustomerID, CustomerName, OrderTypeName

SQL-Server(>=2005) 中的另一种方法是使用 CTE(或子查询) + ROW_NUMBER,然后您可以选择所有字段:

WITH CTE AS
(

    SELECT tblOrder.CustomerID,
           CustomerName,
           OrderTypeName,
           Count(*) OVER (PARTITION BY tblOrder.CustomerID, CustomerName, OrderTypeName),
           RN = ROW_NUMBER() OVER (PARTITION BY tblOrder.CustomerID, CustomerName, OrderTypeName 
                                   ORDER BY CustomerID, CustomerName)
    FROM tblOrder
    INNER JOIN tblCustomer ON tblOrder.CustomerID=tblCustomer.CustomerID
    INNER JOIN tblOrderType ON tblOrder.OrderTypeID=tblOrderType.OrderTypeID
)
SELECT * FROM CTE
WHERE RN = 1
于 2015-04-27T13:10:34.403 回答