SQL 复杂选择 - 形成查询时遇到问题
我有三个表:客户、销售和产品。
Sales 将 CustomerID 与 ProductID 链接起来并具有 SalesPrice。
select Products.Category, AVG(SalePrice) from Sales
inner join Products on Products.ProductID = Sales.ProductID
group by Products.Category
这让我可以看到按类别划分的所有销售的平均价格。但是,我只想包含数据库中拥有 3 条以上销售记录或更多记录的客户。
我不确定最好的方法或任何方法来解决这个问题。有想法吗?
I have three tables, Customers, Sales and Products.
Sales links a CustomerID with a ProductID and has a SalesPrice.
select Products.Category, AVG(SalePrice) from Sales
inner join Products on Products.ProductID = Sales.ProductID
group by Products.Category
This lets me see the average price for all sales by category. However, I only want to include customers that have more than 3 sales records or more in the DB.
I am not sure the best way, or any way, to go about this. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有在任何地方提到客户数据,所以我假设它在销售表中
您需要首先过滤销售表并将其限制为具有超过 3 个销售的客户,然后加入以获取产品类别并获取跨类别的平均值
You haven't mentioned the customer data anywhere so I'll assume it's in the Sales table
You need to filter and restrict the Sales table first to the customers with more the 3 sales, then join to get product category and get the average across categories
我会尝试以下操作:
I'd try the following:
我会使用选择创建一个“大客户 ID”的伪表,然后将其加入到您的查询中以限制结果:
虽然懒得测试这个,所以让我知道它是否有效;o)
I'd create a pseudo-table of "big customer IDs" with a select, and then join it to your query to limit the results:
Too lazy to test this out though, so let me know if it works ;o)
另一种方式
Another way