2017-07-08 54 views
0

目标。为了显示产品名称(产品),产品类型(ProductTypes),销售每个产品的总数(销售部)可能高级的SQL查询3个表的UNION

这里是我的表的查询: enter image description here

我有搞清楚真正的困难了解我是如何做到这一点的。我试图做一个联盟和其他一些事情,但不能得到它的工作。 我可以通过使用这个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 我有这现在显示这个,只需要以某种方式加入销售计数。

enter image description here

+0

加入产品表producttype表终于离开加盟销售表,您刚才提到 –

+0

@ ps_prakash02好感谢您的答复select语句。我现在有这样做你所说的: '''选择Products.ProductName,ProductTypes.ProductType 从产品 INNER JOIN ProductTypes ON Products.ProductTypeID = ProductTypes.ProductTypeID '''仍有点困惑你的意思是左加入销售表。 – Rick1990

+0

从任何基本书籍或教程开始。你会明白你不需要一个UNION。 – Strawberry

回答

1

尝试:

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