【问题标题】:Find the row with the highest value in SQL在 SQL 中查找具有最高值的行
【发布时间】:2019-04-11 14:21:47
【问题描述】:

根据架构,Orders 表有以下列:

OrderId integer, CustomerId integer, RetailerId integer, ProductId integer, Count integer

我试图找出每个零售商的订单数量最多的产品。

所以,总结每个产品的所有订单,我有以下查询:

SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
FROM Orders
GROUP BY RetailerId, ProductId
ORDER BY RetailerId, ProductTotal DESC;

这给出了这样的输出:

RETAILERID  PRODUCTID PRODUCTTOTAL
---------- ---------- ------------
         1          5          115
         1         10           45
         1          1           15
         1          4            2
         1          8            1
         2          9           12
         2         11           10
         2          7            1
         3          3            3
         4          2            1
         5         11            1

现在,我要做的就是找到每个零售商的订单数量最多的产品。现在,它显示了所有产品;我只想要一个。

我尝试了太多东西。下面是一个特别可恶的例子:

SELECT O.RetailerId, O.ProductId, O.ProductTotal
FROM (
  SELECT Orders.ProductId, MAX(ProductTotal) AS MaxProductTotal
  FROM (
    SELECT Orders.ProductId AS PID, SUM(Orders.Count) AS ProductTotal
    FROM Orders
    GROUP BY Orders.ProductId
  ) AS O INNER JOIN Orders ON Orders.ProductId = PID
  GROUP BY Orders.ProductId
) AS X INNER JOIN O ON O.RetailerId = X.RetailerId AND O.ProductTotal = X.MaxProductTotal;

解决这个问题可能是有史以来最简单的事情,但我现在不能。所以,我需要一些帮助。

【问题讨论】:

    标签: sql oracle greatest-n-per-group


    【解决方案1】:

    使用窗口函数选择每个客户的最大总数:

    SELECT RetailerId, ProductId, ProductTotal
    FROM
    (
      SELECT
        RetailerId, ProductId, SUM(Count) AS ProductTotal,
        MAX(SUM(Count)) OVER (PARTITION BY RetailerId) AS MaxProductTotal
      FROM Orders
      GROUP BY RetailerId, ProductId
    )
    WHERE ProductTotal = MaxProductTotal
    ORDER BY RetailerId;
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用窗口函数row_number()

      select * from
      (
      select *,row_number() over(partition by RetailerId order by ProductTotal desc) as rn from
      (
      SELECT RetailerId, ProductId, SUM(Count) AS ProductTotal
      FROM Orders
      GROUP BY RetailerId, ProductId
      )A
      )X where rn=1
      

      【讨论】:

        【解决方案3】:

        您可以将row_number() 与 cte 一起使用

        with cte as
        (
           SELECT o.RetailerId,o.ProductId AS PID, SUM(o.Count) AS ProductTotal
            FROM Orders o
            GROUP BY o.ProductId,o.RetailerId
        ), cte2 as
        (
        select RetailerId,PID,ProductTotal, 
        row_number() over(partition by RetailerId order by ProductTotal desc) as rn
        from cte
        ) select RetailerId,PID,ProductTotal from cte2 where rn=1
        

        【讨论】:

          【解决方案4】:

          你试过使用排名吗?

          试试这个查询

          select OrderC, retailer_id,product_id from ( select sum(count_order) as OrderC,retailer_id,product_id,row_number() over(order by OrderC desc) as RN from order_t  group  by retailer_id,product_id) as d where RN=1;
          

          【讨论】:

            【解决方案5】:

            这是一个没有使用FIRST/LAST 的子查询的版本:

            WITH t(RETAILERID, PRODUCTID, PRODUCTTOTAL) AS (    
                SELECT 1,  5,  115 FROM dual UNION ALL
                SELECT 1, 10,   45 FROM dual UNION ALL
                SELECT 1,  1,   15 FROM dual UNION ALL
                SELECT 1,  4,    2 FROM dual UNION ALL
                SELECT 1,  8,    1 FROM dual UNION ALL
                SELECT 2,  9,   12 FROM dual UNION ALL
                SELECT 2, 11,   10 FROM dual UNION ALL
                SELECT 2,  7,    1 FROM dual UNION ALL
                SELECT 3,  3,    3 FROM dual UNION ALL
                SELECT 4,  2,    1 FROM dual UNION ALL
                SELECT 5,11,    1 FROM dual)
            SELECT 
               RETAILERID, 
               MAX(PRODUCTID) KEEP (DENSE_RANK LAST ORDER BY PRODUCTTOTAL) AS PRODUCTID, 
               MAX(PRODUCTTOTAL)
            FROM t
            GROUP BY RETAILERID;
            
            
            +--------------------------------------+
            |RETAILERID|PRODUCTID|MAX(PRODUCTTOTAL)|
            +--------------------------------------+
            |1         |5        |115              |
            |2         |9        |12               |
            |3         |3        |3                |
            |4         |2        |1                |
            |5         |11       |1                |
            +--------------------------------------+
            

            【讨论】:

              猜你喜欢
              • 2011-03-21
              • 2019-07-12
              • 1970-01-01
              • 1970-01-01
              • 2022-11-02
              • 2017-04-08
              • 1970-01-01
              • 2013-06-23
              • 2020-07-08
              相关资源
              最近更新 更多