Oracle SQL查询:找出总销售额最大的年份

时间:2013-07-28 05:51:12

标签: sql oracle sum max

我的工作表,表名:销售

这是MY TABLE,[sl_no是主键]表结构:

CREATE TABLE  SALES 
  ( SL_NO NUMBER PRIMARY KEY, REGION VARCHAR2(10) NOT NULL, 
    MONTH VARCHAR2(20) NOT NULL, YEAR NUMBER NOT NULL, 
    SALES_AMOUNT NUMBER NOT NULL )

,这是表数据:

SQL> select * from sales;

     SL_NO REGION     MONTH                      YEAR SALES_AMOUNT
---------- ---------- -------------------- ---------- ------------
         1 east       december                   2011       750000
         2 east       august                     2011       800000
         3 west       january                    2012       640000
         5 east       march                      2012      1200000
         6 west       february                   2011       580000
         4 west       april                      2011       555000

6 rows selected.

我已尝试此查询查看[2011,2012]年度的总销售额;

 SELECT year, SUM(sales_amount) FROM sales GROUP BY year;

      YEAR SUM(SALES_AMOUNT)
---------- -----------------
      2011           2685000
      2012           1840000

我的目标:>我想找出最大销售额的年份。 我试过这个,并且完美地工作......但是当 我想要显示那一年 时,它会给出一个错误。

SQL> select  max(sum(sales_amount)) from sales group by year;

MAX(SUM(SALES_AMOUNT))
----------------------
               2685000

SQL> select year, max(sum(sales_amount)) from sales group by year;
select year, max(sum(sales_amount)) from sales group by year
       *
ERROR at line 1:
ORA-00937: not a single-group group function

额外添加:如果多行具有相同的值意味着....当年[2011,2012]的销售额保持不变时,那么......

plZ帮我解决这个问题。

4 个答案:

答案 0 :(得分:0)

以下选择应该做你需要的(未经测试,在家里没有Oracle):

    select year, total
      from (
            select year, sum(sales_amount) total
              from sales
             group by year
           )
     where total = (select max(total_amount)
                     from (
                           select year, sum(sales_amount) total_amount
                             from sales
                            group by year                    
                          ))

但是,考虑到如果其中两个具有完全相同的总量,则每次执行可能会给您不同的年份。您可能希望包含一些更多条件来避免这种情况。

答案 1 :(得分:0)

这应该有用。

with yr_agg as (
  select year, sum(sales_amount) as total
  from sales
  group by year
)
select year, total as max_total
from yr_agg
where total = (select max(total)
               from yr_agg);

答案 2 :(得分:0)

我认为最简单的方法是订购结果并占据第一行:

select year, sales_amount
from (SELECT year, SUM(sales_amount) as sales_amount
      FROM sales
      GROUP BY year
      order by sum(sales_amount) desc
     ) t
where rownum = 1;

编辑:

如果您需要显示所有匹配的行(问题中未提及),我建议使用dense_rank()分析函数:

select year, sales_amount
from (SELECT year, SUM(sales_amount) as sales_amount,
             dense_rank(over order by SUM(sales_amount) desc) as seqnum
      FROM sales
      GROUP BY year
      order by sum(sales_amount) desc
     ) t
where seqnum = 1;

或者,您可能更喜欢max()版本:

select year, sales_amount
from (SELECT year, SUM(sales_amount) as sales_amount,
             max(sum(sales_amount)) over () as maxsa
      FROM sales
      GROUP BY year
      order by sum(sales_amount) desc
     ) t
where sales_amount = maxsa;

答案 3 :(得分:-1)

这是我的查询,其中多行可以选择

SELECT year,MAX(total_sale) as max_total
FROM 
    (SELECT year,SUM(sales_amount) AS total_sale FROM sales GROUP BY year)
GROUP BY 
    year HAVING MAX(total_sale) = 
           (SELECT MAX(total_sale) FROM (SELECT SUM(sales_amount) AS total_sale FROM sales GROUP BY year));