【问题标题】:Find employee who made highest sales to the customer who made most purchases查找对购买最多的客户的销售额最高的员工
【发布时间】: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


【解决方案1】:

尝试逐步解决问题。按总数查找订单最多的客户 ID:

SELECT TOP 1 sales.customerid
FROM sales
JOIN products ON sales.productid = products.productid
GROUP BY sales.customerid
ORDER BY SUM(sales.quantity * products.price) DESC

下一步是按该客户的数量查找销售额最多的员工(将其更改为按总数是微不足道的):

SELECT TOP 1 sales.salespersonid
FROM sales
WHERE sales.customerid = (
    SELECT TOP 1 sales.customerid
    FROM sales
    JOIN products ON sales.productid = products.productid
    GROUP BY sales.customerid
    ORDER BY SUM(sales.quantity * products.price)
)
GROUP BY sales.salespersonid
ORDER BY COUNT(sales.salesid) DESC

最后选择员工记录:

SELECT *
FROM employee
WHERE employeeid = (
    SELECT TOP 1 sales.salespersonid
    FROM sales
    WHERE sales.customerid = (
        SELECT TOP 1 sales.customerid
        FROM sales
        JOIN products ON sales.productid = products.productid
        GROUP BY sales.customerid
        ORDER BY SUM(sales.quantity * products.price)
    )
    GROUP BY sales.salespersonid
    ORDER BY COUNT(sales.salesid) DESC
)

【讨论】:

    【解决方案2】:

    也许是这样的......

    首先获取购买最多的客户,然后找到所有已向该客户销售的员工,并返回销售额最高的前 1 名员工。

    SELECT TOP (1)  
              e.EmploeeId
            , SUM(s.quantity * p.Price) TotalSales
    FROM        Emploees    e 
    inner join  Sales       s ON e.EmploeeId = s.EmploeeId
    inner join  Product     p ON s.productId = s.productId
    WHERE s.CustomerId = (
                            -- Get the customer with most purchases 
                            SELECT TOP (1) x.CustomerId
                            FROM ( SELECT  
                                          c.CustomerId
                                        , SUM(s.quantity * p.Price) TotalSales
                                    FROM        Customers   c 
                                    inner join  Sales       s ON c.CustomerId = s.CustomerId
                                    inner join  Product     p ON s.productId = o.productId
                                    GROUP BY c.CustomerId
                                 ) x
                            ORDER BY TotalSales DESC
                         )
    GROUP BY e.EmploeeId
    ORDER BY TotalSales DESC
    

    要按计数(销售/购买数量)查找最多的销售和购买,以下查询就可以了:

    SELECT TOP (1)  
              e.EmploeeId
            , COUNT(*) TotalSales
    FROM        Emploees    e 
    inner join  Sales       s ON e.EmploeeId = s.EmploeeId
    WHERE s.CustomerId = (
    
                            SELECT TOP (1) x.CustomerId
                            FROM ( SELECT  
                                          c.CustomerId
                                        , COUNT(*) TotalSales
                                    FROM        Customers   c 
                                    inner join  Sales       s ON c.CustomerId = s.CustomerId
                                    GROUP BY c.CustomerId
                                 ) x
                            ORDER BY TotalSales DESC
                         )
    GROUP BY e.EmploeeId
    ORDER BY TotalSales DESC
    

    【讨论】:

    • 感谢您的帮助。
    【解决方案3】:

    在您所说的其中一个 cmets 中,“最多销售”意味着更多的销售数量。这个答案考虑了这个标准。

    SELECT TOP (1)  SalesPersonID,
                    (FirstName + ' ' + MiddleName + ' ' + LastName) AS EmployeeName
    FROM            Sales       S
    JOIN            Employees   E       ON      S.SalesPersonID  =  E.EmployeeID
    WHERE           CustomerID = 
    
    (
    -- Sub-query that returnes CustomerID with most quantities bought 
    SELECT TOP (1)      CustomerID
    FROM                Sales
    GROUP BY            CustomerID
    ORDER BY            SUM(Quantity)       DESC
    )
    
    GROUP BY        SalesPersonID,
                    (FirstName + ' ' + MiddleName + ' ' + LastName)
    ORDER BY        SUM(Quantity)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-21
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多