Oracle 10g ROLLUP和TO_CHAR函数

时间:2013-08-15 14:50:46

标签: oracle rollup

我有2个查询:

select zam_klt_id,zam_order_date, count(*) as sum from orders
group by rollup (zam_order_date,zam_klt_id);

产生输出:

ZAM_KLT_ID ZAM_order_date        SUM
---------- ------------------- ----------
  1002 98/03/13                     1 
       98/03/13                     1 
  1004 98/03/14                     1 
       98/03/14                     1 
  1003 98/09/11                     1 
       98/09/11                     1 
  1003 99/01/05                     1 
       99/01/05                     1 
  1003 99/03/01                     1 
       99/03/01                     1 
  1003 99/07/26                     1 
       99/07/26                     1 
  1003 99/10/30                     1 
       99/10/30                     1 
  1002 00/05/08                     1 
       00/05/08                     1 
  1004 00/06/14                     1 
       00/06/14                     1 
       00/07/12                     1 
       00/07/12                     1 
  1000 00/12/10                     2 
       00/12/10                     2 
  1004 00/12/21                     1 
       00/12/21                     1 
                                   13 

这没关系,在每个日期都有简短的摘要(计数),如

  1000 00/12/10                     2 
       00/12/10                     2 

然而,我想知道每年有多少客户订单,所以我更改了preveious查询(zam_order_date更改为to_char(zam_order_date,'yyyy'))

select zam_klt_id,to_char(zam_order_date,'yyyy'), count(*) as sum from orders
group by rollup ((to_char(zam_order_date,'yyyy'),zam_klt_id));

将产生输出

ZAM_KLT_ID TO_CHAR(ZAM_order_date,'YYYY')        SUM
---------- ----------------------------------- ----------
  1002 1998                                         1 
  1003 1998                                         1 
  1004 1998                                         1 
  1003 1999                                         4 
       2000                                         1 
  1000 2000                                         2 
  1002 2000                                         1 
  1004 2000                                         2 
                                                   13 

9 rows selected 

这次在每个日期(本例中为年份)下都没有摘要,我认为输出应该是这样的:

ZAM_KLT_ID TO_CHAR(ZAM_order_date,'YYYY')        SUM
---------- ----------------------------------- ----------
  1002 1998                                         1 
  1003 1998                                         1 
  1004 1998                                         1 
      *1998*                                         *3*
etc

为什么这次没有添加摘要,是否与to_char函数有关?

0 个答案:

没有答案