分组技术SUM数据库

时间:2017-04-19 13:52:45

标签: oracle sum grouping

早上好,我是一名初级数据库程序员。

我想以最短的方式编写此代码。

我想表示我这样执行

fyear     money
<2012     4
2012      5
2013      5
2014      5
total     19

我有这样的代码

SELECT fyear,SUM(money)
FROM (

          SELECT       CASE 
                         WHEN fyear < 2012 THEN ' <2012'
                         WHEN fyear = 2012 THEN   '2012'
                         WHEN fyear = 2013 THEN   '2013'
                         WHEN fyear = 2014 THEN   '2014'
                         END fyear, money

              FROM( 
                    SELECT fyear, money
                    FROM   table1
                    UNION ALL
                    SELECT fyear, money
                    FROM   table2
                   )
 )GROUP BY fyear

上面的代码

fyear     money
<2012     4
2012      5
2013      5
2014      5

总共缺少这个。我不确定我如何添加“total”,在代码中添加“total”的最佳方法是什么?

非常感谢

2 个答案:

答案 0 :(得分:2)

您可能需要ROLLUP

select NVL(fyear, 'total') fyear, money 
from (
      SELECT fyear, SUM(money) money
      FROM (SELECT CASE
                      WHEN fyear < 2012 THEN '<2012'
                      WHEN fyear = 2012 THEN '2012'
                      WHEN fyear = 2013 THEN '2013'
                      WHEN fyear = 2014 THEN '2014' 
                   END fyear, money
              FROM (SELECT fyear, money FROM table1
                    UNION ALL
                    SELECT fyear, money FROM table2
                   )
           )
      GROUP BY rollup(fyear)
     )

给出:

FYEAR       MONEY
------ ----------
<2012           4
2012            5
2013            5
2014            5
total          19

顺便说一句,根据您的情况,您的CASE可能会简化:

CASE
   WHEN fyear < 2012 THEN '<2012'
   ELSE to_char(fyear)
END 

答案 1 :(得分:1)

您可以使用联合添加总行,如下所示;

with line_tots AS
(SELECT fyear, SUM (curr_qty) as curr_qty
FROM   (SELECT CASE
                  WHEN fyear < 2012 THEN ' <2012'
                  WHEN fyear = 2012 THEN '2012'
                  WHEN fyear = 2013 THEN '2013'
                  WHEN fyear = 2014 THEN '2014'
               END
                  fyear,
               curr_qty
        FROM  table_1)
GROUP BY fyear
ORDER BY fyear)
SELECT *
  FROM (
        SELECT fyear, curr_qty
        FROM line_tots
        UNION ALL
        SELECT 'total', SUM(curr_qty)
        FROM line_tots
        )
ORDER BY fyear 

(我删除了从两个表中选择的子查询,因为它不是解决方案所必需的)

如果您使用的是SQL * Plus,那么另一种方法是使用计算报告列,不需要更改您的查询。

SQL> compute sum label total of curr_qty on report
SQL> break on curr_qty on report
SQL> l
  1  SELECT fyear, SUM (curr_qty) as curr_qty
  2  FROM   (SELECT CASE
  3                    WHEN fyear < 2012 THEN ' <2012'
  4                    WHEN fyear = 2012 THEN '2012'
  5                    WHEN fyear = 2013 THEN '2013'
  6                    WHEN fyear = 2014 THEN '2014'
  7                 END
  8                    fyear,
  9                 curr_qty
 10          FROM  table_1)
 11  GROUP BY fyear
 12* ORDER BY fyear
SQL> /

FYEAR    CURR_QTY
------ ----------
 <2012         13
2012           10
2013           12
2014            9
       ----------
total          44