获得最大的价格上涨

时间:2013-04-06 17:23:52

标签: sql

我在弄清楚这个问题时遇到了问题 我需要找到价格涨幅最大的项目。 这是我的代码:

    Select
  q.Grocery_Item,
  TO_CHAR(q.Price_IN_2000, '$90.00') price_in_2000,
  TO_CHAR(q.Estimated_Price_In_2025, '$90.00') price_in_2025,
  TO_CHAR(w.MY_OUTPUT,'9,990') || '%' as My_Output
From
  GROCERY_PRICES q,
  (SELECT
     GROCERY_ITEM,
     (((Estimated_Price_In_2025-Price_IN_2000)/Price_IN_2000)*100) as MY_OUTPUT
   FROM
     GROCERY_PRICES
  ) w
Where
  q.GROCERY_ITEM = w.GROCERY_ITEM  and w.my_output in (select max(w.my_output) from GROCERY_PRICES group by grocery_item)
GROUP BY
 w.MY_OUTPUT, q.GROCERY_ITEM,  q.Price_IN_2000, q.Estimated_Price_In_2025

我的输出是:

    GROCERY_ITEM                   PRICE_IN_2000 PRICE_IN_2025 MY_OUTPUT
------------------------------ ------------- ------------- ---------
B_001                            $0.80         $2.64          230%   
B_002                            $2.72         $7.36          171%   
M_004                            $2.70         $5.65          109%   
T_006                            $5.70         $6.65           17%   
R_003                            $4.00        $13.20          230%   
E_001                            $0.62         $1.78          187%   

 6 rows selected 

基本上是我表中的所有数据

我需要得到的是:

GROCERY_ITEM                   PRICE_IN_2000 PRICE_IN_2025 MY_OUTPUT
------------------------------ ------------- ------------- ---------
B_001                            $0.80         $2.64          230%   
R_003                            $4.00        $13.20          230%   

对不起新来的,不知道如何格式化我的问题:(

提前感谢。

1 个答案:

答案 0 :(得分:0)

这样的事情:

with price_delta as (
  select grocery_item, 
         price_in_2000, 
         ESTIMATED_PRICE_IN_2025, 
         ((ESTIMATED_PRICE_IN_2025 - Price_IN_2000)/Price_IN_2000)*100 as delta,
         rank() over (order by ((ESTIMATED_PRICE_IN_2025 - Price_IN_2000)/Price_IN_2000)*100 desc) as rnk
  from grocery_prices
)
select grocery_item, 
       price_in_2000, 
       ESTIMATED_PRICE_IN_2025,
       delta
from price_delta
where rnk = 1;

SQLFiddle(Oracle):http://sqlfiddle.com/#!4/43379/2