查找SQL中具有最高值的行

时间:2018-11-08 04:31:29

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

根据架构,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;

解决方案可能是有史以来最简单的事情,但是我现在不能。所以,我想要一些帮助。

5 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

您可以尝试使用窗口功能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

答案 2 :(得分:0)

您可以将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 与cte一起使用

l1 = ['A','B','C','D','A','B']
l2 = []

答案 3 :(得分:0)

您尝试使用等级吗?

尝试此查询

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;

答案 4 :(得分:0)

以下是使用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                |
+--------------------------------------+