2

我的工作表,表名:sales

这是我的表,[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]的销售额保持不变时,那么....

请帮我解决这个问题。

4

4 回答 4

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);
于 2013-07-28T08:28:57.720 回答
0

以下 select 应该可以满足您的需要(未经测试,家里没有 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                    
                          ))

但请注意,如果其中两个的总金额完全相同,则每次执行可能会给您不同的年份。您可能希望包含更多条件来避免这种情况。

于 2013-07-28T07:14:13.740 回答
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;
于 2013-07-28T13:06:53.493 回答
-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));
于 2014-05-04T06:19:18.627 回答