假设你有3张桌子:
分类
产品
销售
我试图提出一个查询,该查询将生成一个数据表,显示每个类别的总销售数量,例如:
Cat ID | Name | Total Sales -------|------|------------- 1 | Red | 35 2 | Blue | 25
对于奖励积分,请添加WHERE子句,以便在“销售日期”的特定日期范围内进行选择。列。
我正在使用SQL Server。
答案 0 :(得分:1)
select Categories.CatID, Categories.Name, count(*) as TotalSales
from Categories
join Products ON Categories.CategoryID = Products.CategoryID
join Sales ON Products.ProductID = Sales.ProductID
WHERE Sales.Saledate BETWEEEN date1 and date2
GROUP BY Categories.CatID, Categories.Name
答案 1 :(得分:1)
对于SQL-Server可能是这样的:
SELECT c.CategoryID AS [Cat ID],
c.Name AS [Name],
COUNT(s.ProductID) AS [Total Sales]
FROM Categories c
LEFT JOIN Products p
ON c.CategoryID = p.CategoryID
LEFT JOIN Sales s
ON p.ProductID = s.ProductID
WHERE s.Saledate BETWEEEN startDate and endDate
GROUP BY c.CategoryID, c.Name