Oracle SQL 从 case 语句中获取最小数量

Oracle SQL to get min number from case statement

我在下面有这个查询(在 Oracle PL/SQL 中),我正在尝试拉价,它似乎给了我 3 条记录,但我只想要一条。如果我 运行 进入这种情况,我想显示最低利率,我可以在下面的 case 语句中的 'rate' 字段周围放置一个 min 函数吗?

  select /*+ rule */ a.skucode as skucode,a.sizecode as 
        sizecode,b.colourdescription as colourdesc, a.season as season, 
        (case when sp.eventnbr in (select eventnbr from event where sysdate       
        between eventbegints 
        and eventendts) then rate else sellprice end) as listprice 
                  from sku a, colour b, skuprcevnt sp 
                  where a.skucode = '00000000051361264-04'
                 --" and a.storecode = '00000' " +
                  and a.storecode = '00000'
                  and a.colourcode = b.colourcode 
                  and a.skucode=sp.skucode(+) 
                  order by a.skucode, a.sizecode, b.colourdescription;

这给出了以下结果(但我只想看到 76.99 的价格):

SKUCODE             SIZECODE   COLOURDESC   SEASON  LISTPRICE
00000000051361264-04    XL     BLACK        FA-13       155
00000000051361264-04    XL     BLACK        FA-13       155
00000000051361264-04    XL     BLACK        FA-13       76.99

select 子句的内容永远不会影响查询返回的行数。您需要在 WHERE 子句中使用 WHERE rate = (select min(rate) sku where skucode=a.skucode ) 的过滤器。

您的示例更复杂,因此实际过滤器会更大,但这只是它的外观的一个想法。

如果对查询进行适当分组,则可以在列列表中使用 MIN:

select a.skucode,
       a.sizecode,
       b.colourdescription as colourdesc,
       a.season, 
       MIN(case
             when sp.eventnbr in (select eventnbr
                                    from event
                                    where sysdate between eventbegints 
                                                      and eventendts)
               then rate
             else sellprice
           end) as listprice 
  from sku a
  INNER JOIN colour b
    ON b.colourcode = a.colourcode 
  LEFT OUTER JOIN skuprcevnt sp
    ON sp.skucode = a.skucode
  where a.skucode = '00000000051361264-04' and
        a.storecode = '00000'
  GROUP BY a.SKUCODE, a.SIZECODE, b.COLOURDESCRIPTION, a.SEASON
  order by a.skucode, a.sizecode, b.colourdescription, a.SEASON;

分享和享受。