我正在尝试从两个表 ng serve -o
和SaleInvoices
中获取数据。两者都与外键Customers
链接。我试图让客户重复记录计数并将其分组。但是它会产生以下错误。
CustomerId
我遇到的错误是
选择列表中的'SaleInvoices.InvoiceID'列无效,因为它不包含在汇总函数或 GROUP BY子句。
答案 0 :(得分:2)
具有SELECT中所有列分组的版本:
Select SaleInvoices.InvoiceID
,SaleInvoices.CustomerID
,Customers.ContactName
,Customers.CNIC
,Customers.City
,Customers.CellNumber
,Customers.CompanyName
,Count(*)
From SaleInvoices
INNER JOIN Customers
ON SaleInvoices.CustomerID=Customers.CustomerId
Where SaleInvoices.UpdatedDate >= '2017-01-02 16:53:53.253'
AND SaleInvoices.UpdatedDate<= '2019-01-02 16:53:53.253'
GROUP BY SaleInvoices.InvoiceID
,SaleInvoices.CustomerID
,Customers.ContactName
,Customers.CNIC
,Customers.City
,Customers.CellNumber
,Customers.CompanyName
从选择中删除列的版本,而无需更改组,方法是:
Select Customers.ContactName
,Count(*)
From SaleInvoices
INNER JOIN Customers
ON SaleInvoices.CustomerID=Customers.CustomerId
Where SaleInvoices.UpdatedDate >= '2017-01-02 16:53:53.253'
AND SaleInvoices.UpdatedDate<= '2019-01-02 16:53:53.253'
GROUP BY Customers.ContactName
另一种方法是在选择的所有列上使用诸如MIN(),MAX()等聚合函数:
Select MIN(SaleInvoices.InvoiceID)
,MIN(SaleInvoices.CustomerID)
,Customers.ContactName
,MIN(Customers.CNIC)
,MIN(Customers.City)
,MIN(Customers.CellNumber)
,MIN(Customers.CompanyName)
,Count(*)
From SaleInvoices
INNER JOIN Customers
ON SaleInvoices.CustomerID=Customers.CustomerId
Where SaleInvoices.UpdatedDate >= '2017-01-02 16:53:53.253'
AND SaleInvoices.UpdatedDate<= '2019-01-02 16:53:53.253'
GROUP BY Customers.ContactName
答案 1 :(得分:0)
您正在按Customers.ContactName
对结果集的输出进行分组,因此,只能在Customers.ContactName
中包含select
。
您有两种选择:
1)汇总您在select
中放入的所有列。
2)或者让他们所有人都参加了group by
子句。