[英]Possibly advanced SQL query UNION of 3 Tables
目标。 进行查询以显示产品名称(产品),产品类型(ProductTypes)和每种产品的销售总数(销售)
我确实很难弄清楚我该怎么做。 我正在尝试做一个UNION和其他一些事情,但是无法使其正常工作。 我可以通过使用此SELECT ProductID, count(*) as NumSales from Sales group by ProductID
来获得销售总数,以SELECT ProductID, count(*) as NumSales from Sales group by ProductID
但实际上很难解决其余问题并正确格式化它。 任何帮助,将不胜感激。
编辑: Select Products.ProductName, ProductTypes.ProductType From Products INNER JOIN ProductTypes ON Products.ProductTypeID=ProductTypes.ProductTypeID
我现在要显示此信息,只需要以某种方式加入销售数量即可。
尝试:
select prod.ProductName, ptyp.ProductType, count(SaleID) count_sale
from Products prod
join ProductTypes ptyp on ( ptyp.ProductTypeID= prod.ProductTypeID)
join Sales sal on ( sal.ProductID = prod.ProductID)
group by prod.ProductName, ptyp.ProductType
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.