如何按升序选择每个类别的最高价格的行?

Posted

技术标签:

【中文标题】如何按升序选择每个类别的最高价格的行?【英文标题】:How to select row that have the max price of each category in asc order? 【发布时间】:2021-05-27 00:17:23 【问题描述】:

下面是我的表格,其中包含以下项目:

ProductId ProductName Category Price
13 Cadbury C-1 $12.00
21 Nestle C-1 $13.99
73 Amul C-101 $9.00
43 Ghirardelli C-101 $10.88
105 Snickers C-2 $18.90
677 Amul C-3 $30.19
107 Kit Kat C-2 $2.00
899 Ferrero Rocher C-4 $5.00
209 Spy C-3 $4.00
1014 Naviluna C-1 $13.99
1561 Twix C-101 $10.88

我想要的输出是:-

ProductId ProductName Category Price
21 Nestle C-1 $13.99
105 Snickers C-2 $18.90
677 Amul C-3 $30.19
899 Ferrero Rocher C-4 $5.00
43 Ghirardelli C-101 $10.88

如果最高价格相同,则按类别 asc 查找具有最高价格和最低 product_id 组的行

【问题讨论】:

。 .我删除了不一致的数据库标签。请仅使用您真正使用的数据库进行标记。 【参考方案1】:

猜测是这样的:

设置测试数据

create table prices (productid, productname, category, price) as
select   13, 'Cadbury'       , 'C-1'  , '$12.00' from dual union all
select   21, 'Nestle'        , 'C-1'  , '$13.99' from dual union all
select   73, 'Amul'          , 'C-101',  '$9.00' from dual union all
select   43, 'Ghirardelli'   , 'C-101', '$10.88' from dual union all
select  105, 'Snickers'      , 'C-2'  , '$18.90' from dual union all
select  677, 'Amul'          , 'C-3'  , '$30.19' from dual union all
select  107, 'Kit Kat'       , 'C-2'  ,  '$2.00' from dual union all
select  899, 'Ferrero Rocher', 'C-4'  ,  '$5.00' from dual union all
select  209, 'Spy'           , 'C-3'  ,  '$4.00' from dual union all
select 1014, 'Naviluna'      , 'C-1'  , '$13.99' from dual union all
select 1561, 'Twix'          , 'C-101', '$10.88' from dual
;

查询和输出

with prep as (
  select productid, productname, category, price,
         to_number(price, '$999.99') as num_price
  from   prices
)
select min(productid) keep (dense_rank first 
              order by num_price desc nulls last) as productid,
       min(productname) keep (dense_rank first
              order by num_price desc nulls last, productid) as productname,
       category,
       max(price) keep (dense_rank first 
              order by num_price desc nulls last) as price
from   prep
group  by category
order  by to_number(substr(category, instr(category, '-') + 1))
;

PRODUCTID PRODUCTNAME    CATEGORY PRICE 
--------- -------------- -------- ------
       21 Nestle         C-1      $13.99
      105 Snickers       C-2      $18.90
      677 Amul           C-3      $30.19
      899 Ferrero Rocher C-4      $5.00 
       43 Ghirardelli    C-101    $10.88

【讨论】:

【参考方案2】:

你可以使用row_number():

select t.*
from (select t.*,
             row_number() over (partition by category order by price desc, product_id asc) as seqnum
      from t
     ) t
where seqnum = 1;

【讨论】:

嗨@Gordon Linoff,该类别是否按asc 排序?如果是这样,你能解释一下吗? @Sunny 。 . .此答案中没有任何内容按category 排序。当然,这可以添加到外部查询中。

以上是关于如何按升序选择每个类别的最高价格的行?的主要内容,如果未能解决你的问题,请参考以下文章

通过一个 SQL 查询从多个类别中检索最常见的项目名称及其最高价格

如何取到所有股票(包括A股B股)某一天的价格(开盘价、最高价、最低价、收盘价,或其中一个价格)???

按 DESC 和 ASC 排序搜索结果

对 2D ArrayList 进行冒泡排序

Rails 订单产品价格按其最小值 has_many 关系升序和降序

如何找到最高和最低的产品价格