题解 | #每个供应商成本最低的产品#

每个供应商成本最低的产品

https://www.nowcoder.com/practice/f0679d06b50049499691e12a435de8d8

1. 这道题有两种解法,第一是使用窗口函数 row_number() over() 分组得到每个vend_id 的每条价格及按照组内价格由低到高排序的序号,并将这种结果作为内部临时表供外部表查询; 第二种是直接使用vend_id 分组并得到每组的最小价格
min(prod_price) ,最后通过Order by 对 所查询出的min(prod_price) 结果排序; 解法上讲,第二种方法更加简便高效快捷。


/*select t.vend_id,
       t.prod_price cheapest_item 
       from (
select vend_id,
       prod_price ,
       row_number() over(partition by vend_id order by prod_price) rN 
  from Products 
  group by vend_id,prod_price 
       ) t
  where rn = 1 
 order by cheapest_item ; 
 **/
 
 select vend_id ,
        min(prod_price) cheapest_item 
   from Products 
   group by vend_id 
   order by cheapest_item ; 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务