【发布时间】:2019-04-26 17:20:41
【问题描述】:
我在数据库作业中遇到了这个问题,需要回答这个问题:
哪个员工对购买最多的客户的销售额最高?
这些是我编写此查询的尝试
--select Customers.Firstname,Products.Name,Sales.Quantity from Customers
--inner join Sales
--on Customers.CustomerId=Sales.CustomerId
--inner join Products
--on Sales.productId=Products.ProductId
--where Products.Name like 'Mobile'
--Select Customers.CustomerId,max(COUNT(Customers.CustomerId)) As Customecount,Emploees.EmploeeId,max(COUNT(Emploees.EmploeeId))as EmploeeeCount from Emploees
--inner join Sales
--on Emploees.EmploeeId=Sales.EmploeeId
--inner join Customers
--on Customers.CustomerId=Sales.CustomerId
--group by Customers.CustomerId,Emploees.EmploeeId ,Count(Sales.productId)as productCount,Count(Emploees.EmploeeId)as emploeeCount,Count(Customers.CustomerId)as customerCount
select * from
(select Distinct Customers.CustomerId,Sales.productId,COUNT(Sales.productId)as CountProduct from Customers
inner join Sales
on Customers.CustomerId=Sales.CustomerId
inner join Emploees
on Emploees.EmploeeId=Sales.EmploeeId
group by Sales.productId,Emploees.EmploeeId,Customers.CustomerId,Sales.productId) as Result
--gr
但这些不起作用
请帮我写这个查询。
【问题讨论】:
-
这是你的功课,自己做,不要作弊,啧啧
-
我知道。但这非常令人困惑。我尝试了数千种方法。但它不起作用
-
好的,在你的问题中发布你的尝试,让我们看看你到目前为止做了什么。
-
我加了我的努力,看他们。 ;-)
-
购买最多的标准是什么?按数量还是按总量?
标签: sql sql-server group-by aggregate-functions