选择最常用的值和sum

时间:2016-09-14 11:35:56

标签: sql sql-server sql-server-2008 tsql sql-server-2008-r2

我想找到最常见的产品,每个员工与大多数收入的产品一起销售。我写了一些更清晰的例子。

DECLARE @Products TABLE(ID INT, ProductName NVARCHAR(50), Price DECIMAL)
DECLARE @Employees TABLE(ID INT, EmployeeName NVARCHAR(50))
DECLARE @Sales TABLE(ID INT, EmployeeID INT, ProductID INT, Quantity INT)

INSERT INTO @Products VALUES
(1, N'Product1', 300),
(2, N'Product2', 500),
(3, N'Product3', 700),
(4, N'Product4', 800)

INSERT INTO @Employees VALUES
(1, N'Employee1'),
(2, N'Employee2'),
(3, N'Employee3')

INSERT INTO @Sales VALUES
(1,1,2,3),
(2,1,3,4),
(3,1,2,5),
(4,2,2,7),
(5,2,4,3),
(6,3,2,3),
(7,3,2,9),
(8,3,4,8)

联接表看起来像这样:

enter image description here

我写了一些选择

SELECT
     e.EmployeeName
    ,p.ProductName
    ,SUM(s.Quantity) AS Quantity    
    ,p.ProductName
    ,SUM(s.Quantity * p.Price) AS Price
FROM @Products p
    INNER JOIN @Sales s ON s.ProductID = p.ID
    INNER JOIN @Employees e ON s.EmployeeID = e.ID
GROUP BY e.EmployeeName, p.ProductName
ORDER BY SUM(s.Quantity) DESC, SUM(s.Quantity * p.Price)

返回此结果,这是不正确的。您注意到价格未订购 enter image description here

我希望以这种格式编写选择,分别为每个员工订购ProductName和数量和,产品名称和价格总和。

预期输出应为

EmployeeName        ProductName    Quantity    ProductName      Price
------------        -----------    --------    -----------      ------
Employee3           Product2       12          Product4         6400
Employee1           Product2       8           Product2         4000     
Employee3           Product4       8           Product2         6000
Employee2           Product2       7           Product2         3500
Employee1           Product3       4           Product3         2800
Employee2           Product4       3           Product4         2400

3 个答案:

答案 0 :(得分:0)

您的查询是开始。然后你可以使用窗口函数和条件聚合:

SELECT EmployeeName,
       MAX(CASE WHEN seqnum_q = 1 THEN ProductName END) as productname_q,
       MAX(CASE WHEN seqnum_q = 1 THEN Quantity END) as quantity_q,
       MAX(CASE WHEN seqnum_pq = 1 THEN ProductName END) as productname_pq,
       MAX(CASE WHEN seqnum_pq = 1 THEN Quantity END) as quantity_pq
FROM (SELECT e.EmployeeName, p.ProductName,
             SUM(s.Quantity) AS Quantity, SUM(s.Quantity * p.Price) AS Price,
             ROW_NUMBER() OVER (PARTITION BY e.EmployeeName
                                ORDER BY SUM(s.Quantity) DESC
                               ) as seqnum_q,
             ROW_NUMBER() OVER (PARTITION BY e.EmployeeName
                                ORDER BY SUM(s.Quantity * p.Price) DESC
                               ) as seqnum_pq
      FROM @Products p INNER JOIN
           @Sales s
           ON s.ProductID = p.ID INNER JOIN
           @Employees e
           ON s.EmployeeID = e.ID
      GROUP BY e.EmployeeName, p.ProductName
     ) ep
GROUP BY EmployeeName;

答案 1 :(得分:0)

将您的订单更改为:

ORDER BY SUM(s.Quantity) DESC, SUM(s.Quantity * p.Price) Desc

答案 2 :(得分:0)

试试这个:

SELECT
     e.EmployeeName
    ,p.ProductName
    ,SUM(s.Quantity) AS Quantity    
    ,p.ProductName
    ,SUM(s.Quantity * p.Price) AS Price
FROM @Sales s
    INNER JOIN @Products p  ON s.ProductID = p.ID
    INNER JOIN @Employees e ON s.EmployeeID = e.ID

GROUP BY e.EmployeeName, p.ProductName
ORDER BY e.EmployeeName, SUM(s.Quantity) DESC, SUM(s.Quantity * p.Price),p.ProductName