<sql>如何将字母添加到结果数据</sql>

时间:2013-07-16 04:35:26

标签: sql oracle

以下是我对月度数据的查询。

select
  NVL(MONTH, 'SUM) MONTH
   , sum(A) AS A, sum(B) AS B
 from
  (
    select b.* 
        from TABLE  b
    where b.MONTH between '201305' and '201306'
  )
    group by ROLLUP(MONTH)

我得到像

这样的数据
 |  MONTH  | 

   201305

   201306

但我希望数据的名称如下所示。

  |  MONTH  | 

 2013. 05 MONTH

 2013. 06 MONTH

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

如何简单地使用CASE声明?

SELECT
  CASE WHEN MONTH IS NULL THEN 'SUM'
  ELSE SUBSTR(NVL(MONTH, 'SUM'),1,4) || '. ' || 
       SUBSTR(NVL(MONTH, 'SUM'),5) || ' MONTH' END MONTH
   , SUM(A) AS A, SUM(B) AS B
FROM
  (
    SELECT b.* 
        FROM  b
    WHERE b.MONTH BETWEEN '201305' AND '201306'
  )
GROUP BY ROLLUP(MONTH)

An SQLfiddle to test with