我有如下一个表table 
dw sj rq 
aa  12 2009-03-27 12:00:00 
ab  13 2009-03-27 11:30:00 
aa  11 2009-03-27 11:05:00 
ab  21 2009-03-26 16:00:00 
aa  sd 2009-03-26 12:00:00 
ac  sf 2009-03-26 11:00:00 
aa  12 2009-03-25 12:00:00 
aa  19 2009-03-20 12:30:00 
aa  25 2009-03-01 12:00:00
aa  cv 2009-01-01 12:00:00 
ca  er 2009-03-27 22:12:12
cb  hg 2009-01-27 22:12:12
我想实现如下结果 
  dw 全部 当日 三日 七日 一个月 
  aa    7    2    4  5    6 
  ab    2    1    2  2    2 
  ac    1    0    1  1    1 
  合计  10 3 7 8 9
  c单位 2    1 0 0 0
  d单位 ……
说明下:第二列“全部”就是“aa”在数据库出现的总次数
合计是对aa,ab,ac……以a开头的数的合计(这个功能是主要想解决的)
同时下面再增加一行,以C开头的或者再增加一行以D开头的单位的项目统计

解决方案 »

  1.   

    上次开贴,已解决问题代码
    我有如下一个表table 
    dw sj rq 
    a  12 2009-03-27 12:00:00 
    b  13 2009-03-27 11:30:00 
    a  11 2009-03-27 11:05:00 
    b  21 2009-03-26 16:00:00 
    a  sd 2009-03-26 12:00:00 
    c  sf 2009-03-26 11:00:00 
    a  12 2009-03-25 12:00:00 
    a  12 2009-03-20 12:30:00 
    a  12 2009-03-01 12:00:00 
    我想实现如下结果 
      dw 当日 三日 七日 一个月 
      a  2    4  5    6 
      b 1    2  2    2 
      c 0    1  1    1 代码为
    select 
          dw, 
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table 
    group by dw
      

  2.   

    select 
          dw, 
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table 
    group by dw
    union all
    select 
          substr(dw,1,1) || '单位' dw, 
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table 
    group by substr(dw,1,1) || '单位'
      

  3.   

    我想实现如下结果 
      dw 全部 当日 三日 七日 一个月 
      aa    7    2    4  5    6 
      ab    2    1    2  2    2 
      ac    1    0    1  1    1 
      合计  10 3 7 8 9 
      c单位 2    1 0 0 0 
      d单位 …… 说明下:第二列“全部”就是“aa”在数据库出现的总次数 
    合计是对aa,ab,ac……以a开头的数的合计(这个功能是主要想解决的) 
    同时下面再增加一行,以C开头的或者再增加一行以D开头的单位的项目统计 
    增加“全部”列,“合计”行
    好像上面的代码不行哦 
      

  4.   

    看错.这样呢?select 
          dw, 
          count(1) 全部,
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table 
    group by dw
    with rollup
    union all
    select 
          substr(dw,1,1) || '单位' dw, 
          count(1) 全部,
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table 
    group by substr(dw,1,1) || '单位'
      

  5.   

    还有我想指定增加 
    c开头的单位和d开头的单位 
    不想增加其他字母开头的单位统计select 
          nvl(dw,'合计') dw, 
          count(1) 全部,
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table 
    group by dw
    with rollup
    union all
    select 
          substr(dw,1,1) || '单位' dw, 
          count(1) 全部,
          sum(case when trunc(sysdate)=trunc(rq) then 1 
                    else 0 
              end 
              ) 当日, 
          sum(case when trunc(sysdate)-trunc(rq) <=3 then 1 
                    else 0 
              end 
              ) 三日, 
          sum(case when trunc(sysdate)-trunc(rq) <=7 then 1 
                    else 0 
              end 
              ) 七日, 
          sum(case when trunc(sysdate)-trunc(rq) <=30 then 1 
                    else 0 
              end 
              ) 一个月 
      from table where substr(dw,1,1) in ('c' , 'd')
    group by substr(dw,1,1) || '单位'
      

  6.   

    感觉还有点问题
    我有如下一个表table 
    dw sj rq 
    aa  12 2009-03-27 12:00:00 
    ab  13 2009-03-27 11:30:00 
    aa  11 2009-03-27 11:05:00 
    ab  21 2009-03-26 16:00:00 
    aa  sd 2009-03-26 12:00:00 
    ac  sf 2009-03-26 11:00:00 
    aa  12 2009-03-25 12:00:00 
    aa  19 2009-03-20 12:30:00 
    aa  25 2009-03-01 12:00:00 
    aa  cv 2009-01-01 12:00:00 
    ad  er 2008-03-27 22:12:12 (注意这个时间)
     
    我想实现如下结果 
      dw 全部 当日 三日 七日 一个月 
      aa    7    2    4  5    6 
      ab    2    1    2  2    2 
      ac    1    0    1  1    1
      ad    0    0    0  0    0  
      合计  10 3 7 8 9 
    如果我想统计今年以来2009-01-01的数据,但是ad在今年没有数据,我又想他在表上有名称显示,按照上面的查询好象显示不出的,不知道如何解决
      

  7.   

    (select distinct dw from table)
    然后再和原来的结果做左关联
      

  8.   

    左关联怎么做呢,还有合计该怎么写oracle里面,用 “select 
          nvl(dw,'合计') dw,”好像报错的
      

  9.   

    那就简单些:
    case when ... and trunc(rq,'y')=trunc(sysdate,'y') then 1 else 0
      

  10.   

    把时间限制加在这里有没有试过:
    case when ... and trunc(rq,'y')=trunc(sysdate,'y') then 1 else 0
      

  11.   

    这个没有试过,我是菜鸟哦,case when 还不知道怎么用呢
    现在关键是合计怎么计算,不知道怎么写了
      

  12.   

    我有如下一个表table 
    dw sj rq 
    aa  12 2009-03-27 12:00:00 
    ab  13 2009-03-27 11:30:00 
    aa  11 2009-03-27 11:05:00 
    ab  21 2009-03-26 16:00:00 
    aa  sd 2009-03-26 12:00:00 
    ac  sf 2009-03-26 11:00:00 
    aa  12 2009-03-25 12:00:00 
    aa  19 2009-03-20 12:30:00 
    aa  25 2009-03-01 12:00:00 
    aa  cv 2009-01-01 12:00:00 
    ca  er 2009-03-27 22:12:12 
    cb  hg 2009-01-27 22:12:12 
    我想实现如下结果 
      dw 全部 当日 三日 七日 一个月 
      aa    7    2    4  5    6 
      ab    2    1    2  2    2 
      ac    1    0    1  1    1 
      合计  10 3 7 8 9 如果单独求合计这一行,SQL的代码怎么写