我尝试根据存储在我的数据库中的数据生成报告,但我发现了错误
Msg 8120,Level 16,State 1,Line 1
专栏' Production.ProductCategory.Name'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
我的查询如下
SELECT
pc.Name AS Categoria,
DATEPART(YEAR, soh.OrderDate) AS [ano],
SUM((det.UnitPrice - det.UnitPriceDiscount) * det.OrderQty) AS total,
SUM(soh.Freight) AS transporte,
SUM(det.LineTotal) AS vendas
FROM
Sales.SalesOrderHeader AS soh
INNER JOIN
Sales.SalesPerson AS sp ON sp.BusinessEntityID = soh.SalesPersonID
INNER JOIN
Sales.SalesOrderDetail AS det ON soh.SalesOrderID = det.SalesOrderID
INNER JOIN
HumanResources.Employee AS e ON soh.SalesPersonID = e.BusinessEntityID
INNER JOIN
Person.Person AS per ON per.BusinessEntityID = sp.BusinessEntityID
INNER JOIN
Production.Product AS p ON det.ProductID = p.ProductID
INNER JOIN
Production.ProductSubcategory AS ps ON p.ProductSubcategoryID = ps.ProductSubcategoryID
INNER JOIN
Production.ProductCategory AS pc ON ps.ProductCategoryID = pc.ProductCategoryID
GROUP BY
DATEPART(YEAR, soh.OrderDate)
ORDER BY
DATEPART(YEAR, soh.OrderDate);
我想只有一件配饰/衣服/自行车等,有一次2011/2012等...
答案 0 :(得分:3)
更改此行:
group by DATEPART(YEAR, soh.OrderDate)
到这个
group by DATEPART(YEAR, soh.OrderDate), pc.Name
答案 1 :(得分:1)
添加
GROUP BY pc.Name
到查询的底部