Oracle Query选择正确的记录

时间:2014-05-22 04:49:33

标签: sql oracle

我一直在考虑如何根据特定数量选择正确的记录。

样本表如下

select b.*  from
(
    SELECT 100 as min_qty, '50' as price, '123' as itemfrom dual
    UNION ALL
    SELECT 150 as min_qty, '200' as price , '123' as item from dual
    UNION ALL
    SELECT 200 as min_qty, '300' as price, '123' as item from dual
    UNION
      SELECT 50 as min_qty, '300' as price, '345' as item from dual
    UNION ALL
    SELECT 150 as min_qty, '200' as price , '345' as item from dual
    UNION ALL
    SELECT 300 as min_qty, '100' as price, '345' as item from dual

   ) b

所以,问题是如果项目是345并且数量是240,那么价格是多少?

对我的解释感到抱歉,我的英语不太好。

1 个答案:

答案 0 :(得分:0)

一个选项是

SELECT price
  FROM (SELECT min_qty,
               item,
               price,
               lead(min_qty) over (partition by item 
                                       order by min_qty) max_qty
         FROM your_table)
 WHERE item = '345'
   AND 240 between min_qty and max_qty