2

我的问题需要我从表中查询数据,并包含一列来计算增加百分比。我只需要使用 MAX 提取增长百分比最高的记录。我认为我在正确的轨道上,但由于某种原因,它返回所有记录,尽管有条款只要求最大值。

Select
  O.Grocery_Item,
  TO_CHAR(sum(g.Price_IN_2000), '$99,990.00') TOTAL_IN_2000,
  TO_CHAR(sum(g.Estimated_Price_In_2025), '$99,990.00') TOTAL_IN_2025,
  TO_CHAR(Round(O.MY_OUTPUT),'9,990') || '%' as My_Output
From
  GROCERY_PRICES g,
  (SELECT
     GROCERY_ITEM,
     (((sum(Estimated_Price_In_2025) -
        sum(Price_IN_2000))/sum(Price_IN_2000))*100) MY_OUTPUT
   FROM
     GROCERY_PRICES
   GROUP BY GROCERY_ITEM) O
Where
  G.GROCERY_ITEM = O.GROCERY_ITEM 
GROUP BY
  O.GROCERY_ITEM, O.MY_OUTPUT
Having
  my_output IN (select Max(O.MY_OUTPUT) from GROCERY_PRICES);

结果:

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

 6 rows selected 
4

2 回答 2

1

You can simplify your query so you only select from the Groceries table once since your My_Output column is only a function of numbers you are already producing the self join is not necessary. Then I've used RANK to get the top records (although if you are not concerned about ties ROWNUM will work better):

SELECT  g.Grocery_Item,
        g.TOTAL_IN_2000,
        g.TOTAL_IN_2025,
        g.My_Output
FROM    (   SELECT  Grocery_Item,
                    TO_CHAR(TOTAL_IN_2000, '$99,990.00') TOTAL_IN_2000,
                    TO_CHAR(TOTAL_IN_2025, '$99,990.00') TOTAL_IN_2025,
                    TO_CHAR(ROUND(((TOTAL_IN_2025 / TOTAL_IN_2000) - 1) * 100), '9,990') || '%' as My_Output,
                    RANK() OVER(PARTITION BY Grocery_Item ORDER BY (TOTAL_IN_2025 / TOTAL_IN_2000) - 1 DESC) AS GroceryRank
            FROM    (   SELECT  g.Grocery_Item,
                                SUM(g.Price_IN_2000) TOTAL_IN_2000,
                                SUM(g.Estimated_Price_In_2025) TOTAL_IN_2025
                        FROM    GROCERY_PRICES g
                        GROUP BY g.Grocery_Item
                    ) g
        ) g
WHERE   GroceryRank = 1;

I've also simplified your percentage calculation.

于 2013-01-21T22:16:34.503 回答
0

试试这个:

select *
from (Select O.Grocery_Item, TO_CHAR(sum(g.Price_IN_2000), '$99,990.00') TOTAL_IN_2000,
             TO_CHAR(sum(g.Estimated_Price_In_2025), '$99,990.00') TOTAL_IN_2025,
             TO_CHAR(Round(O.MY_OUTPUT),'9,990') || '%' as My_Output
      From GROCERY_PRICES g join
           (SELECT GROCERY_ITEM,
                   (((sum(Estimated_Price_In_2025) -
                    sum(Price_IN_2000))/sum(Price_IN_2000))*100
                    ) MY_OUTPUT
            FROM GROCERY_PRICES
            GROUP BY GROCERY_ITEM
           ) O
           on G.GROCERY_ITEM = O.GROCERY_ITEM 
      GROUP BY O.GROCERY_ITEM, O.MY_OUTPUT
      ORDER BY my_output desc
     ) t
where rownum = 1

问题是您的子查询只有外部引用。因此,o.my_output来自外部表,而不是from子查询中的子句。您正在将一个值与其自身进行比较,对于非 NULL 值,它始终为真。

由于您想要最大值,因此最简单的方法是对列表进行排序并取第一行。您也可以使用分析函数执行此操作,但 rownum 通常更有效。

于 2013-01-21T22:09:36.027 回答